我正在尝试应用DefaultTextStyle
,但即使样式已定义并可用(通过调用DefaultTextStyle.of(context).style
建立),它在默认情况下也不会应用到子Text
对象。那么,我做错了什么,或者没有理解呢?
下面是我的调用类中的build
方法,我在其中定义了我的DefaultTextStyle
Widget build(BuildContext context) {
return new MaterialApp(
onGenerateTitle: (BuildContext context) =>
Strings.of(context).getStr('app_title'),
localizationsDelegates: [
const StringsDelegate(),
GlobalWidgetsLocalizations.delegate,
],
localeResolutionCallback: Strings.resolveLocale,
// Watch out: MaterialApp creates a Localizations widget
// with the specified delegates. DemoLocalizations.of()
// will only find the app's Localizations widget if its
// context is a child of the app.
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new DefaultTextStyle(
style: new TextStyle(
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
decorationColor: Colors.red,
decorationStyle: TextDecorationStyle.wavy,
color: Colors.blue
),
child: new StatusPage())
);
}
这里是StatusPage
,我在这里尝试使用DefaultTextStyle
class StatusPage extends MyStatelessWidget {
@override
Widget build(BuildContext context) {
TextStyle style = DefaultTextStyle.of(context).style;
print("STYLE: $style");
return new Scaffold(
appBar: new AppBar(
title: getText(context, 'app_title')
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text('wibble', style:style),
new ActivityStatus(),
new MonitoringStatus()]
)));
}
}
使用所示的代码,文本"wibble“将以适当的样式正确显示。我从文档中得到的理解是,默认情况下应该应用这种样式,因此我不需要为"wibble“的Text
构造函数提供样式参数。
但是,如果删除样式参数,则无法从DefaultTextStyle
中获取样式。
我遗漏了什么?
发布于 2018-04-13 18:42:04
像这样将DefaultTextStyle应用于Scaffold,您将在所有子文本小部件中获得此样式
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new StatusPage());
}
}
class StatusPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
TextStyle style = DefaultTextStyle.of(context).style;
return new Scaffold(
appBar: new AppBar(),
body: new DefaultTextStyle(
style: new TextStyle(
inherit: true,
fontSize: 20.0,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
decorationColor: Colors.red,
decorationStyle: TextDecorationStyle.wavy,
color: Colors.blue),
child: new Center(
child: new Column(
children: <Widget>[
new Text("hello"),
],
),
)));
}
}
发布于 2020-06-11 15:15:28
我以前也遇到过同样的问题,我认为当使用自定义字体或更改语言时(至少我是这样),我的解决方案是转到MaterialApp
小部件,然后覆盖所有的textTheme
属性,如下所示:
fontFamily: "STCBold",
textTheme: GoogleFonts.cairoTextTheme(textTheme).copyWith(
headline1: TextStyle(height: 1),
headline2: TextStyle(height: 1),
headline3: TextStyle(height: 1),
headline4: TextStyle(height: 1),
headline5: TextStyle(height: 1),
headline6: TextStyle(height: 1),
subtitle1: TextStyle(height: 1),
subtitle2: TextStyle(height: 1),
bodyText1: TextStyle(height: 1),
bodyText2: TextStyle(height: 1),
caption: TextStyle(height: 1),
button: TextStyle(height: 1),
overline: TextStyle(height: 1),
),
这将使所有的文本主题没有额外的填充,因此所有的文本将是紧凑的。但请确保在所有代码中使用style: Theme.of(context).textTheme.WHATEVERTEXT.
https://stackoverflow.com/questions/49806705
复制相似问题