几个小时以来,我一直在尝试覆盖我的应用主题,但没有成功,我将非常感激我能获得的任何帮助。
我认为我正在做this questioner的工作,但是对我来说,由于某种原因该主题并未被覆盖,我也不明白为什么。无论我做什么,“嘿”都不是白色。有什么想法吗?谢谢。
@override
Widget build(BuildContext context) {
final ThemeData _themeData = Theme.of(context);
return Theme(
data: _themeData.copyWith(
textTheme: _themeData.textTheme.copyWith(
headline: _themeData.textTheme.headline.copyWith(
color: Colors.white,
),
),
),
child: Center(
child: Text(
'hey',
style: Theme.of(context).textTheme.headline,
),
),
);
}
答案 0 :(得分:0)
Text style
使用-DefaultTextStyle
更新。 Text
不使用主题。
文本使用DefaultTextStyle
,它由MaterialApp(或其他一些小部件,例如AppBar)使用主题中的值进行编辑。
工作代码:
body: Center(
child: DefaultTextStyle(
style: TextStyle().copyWith(
color: Colors.red,
),
child: Text(
'hey', // red color
// style: Theme.of(context).textTheme.headline,
),
),
),
更新:主题级别为MaterialApp
。
class MyHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
textTheme: TextTheme(headline: TextStyle(color: Colors.redAccent))),
home: MyApp());
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final ThemeData _themeData = Theme.of(context);
return Scaffold(
body: Center(
child: Center(
child: Text(
'hey', // red color
style: _themeData.textTheme.headline,
),
),
),
);
}
}
要即时更新:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final ThemeData _themeData = Theme.of(context);
return Scaffold(
body: Center(
child: Center(
child: Text(
'hey', //Green color
style: _themeData.textTheme.headline.apply(color: Colors.green),
),
),
),
);
}
}
答案 1 :(得分:0)
我意识到它不起作用的原因是因为在构建方法之前需要覆盖主题!然后上下文将成为新主题。