我正在尝试应用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()]
)));
}
}
使用如图所示的代码,Text&#34; wibble&#34;正确显示适当的样式。我对文档的理解是,默认情况下应该应用这种样式,所以我不需要Text
构造函数的样式参数用于&#34; wibble&#34;。
但是,如果我删除了样式参数,我就不会从DefaultTextStyle
获取样式。
我错过了什么?
答案 0 :(得分:4)
将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"),
],
),
)));
}
}
答案 1 :(得分:2)
我之前也遇到过同样的问题,我认为使用自定义字体或更改语言时会出现(至少是我的情况)
对我来说,解决方案是转到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.