我正在尝试应用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())
);
}
Run Code Online (Sandbox Code Playgroud)
这是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()]
)));
}
}
Run Code Online (Sandbox Code Playgroud)
使用所示的代码,将以适当的样式正确显示文本“摆动”。我从文档中得出的理解是,默认情况下应应用此样式,因此,我不需要Text
构造器的“自变量” 参数。
但是,如果删除样式参数,则不会从中获取样式DefaultTextStyle
。
我想念什么?
像这样将DefaultTextStyle应用于Scaffold,您将在所有后代Text小部件中获得此样式。
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"),
],
),
)));
}
}
Run Code Online (Sandbox Code Playgroud)
我以前遇到过同样的问题,我认为当使用自定义字体或更改语言时(至少这是我的情况),我的解决方案是使用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),
),
Run Code Online (Sandbox Code Playgroud)
这将使所有文本主题没有额外的填充,因此所有文本都会很紧。style: Theme.of(context).textTheme.WHATEVERTEXT.
但请确保在所有代码中使用
归档时间: |
|
查看次数: |
2865 次 |
最近记录: |