【发布时间】:2021-04-08 15:17:50
【问题描述】:
我正在尝试使用 Theme 小部件来覆盖特定容器的主题样式。
在这里,我将所有文本小部件的正文文本颜色设置为白色。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
textTheme: TextTheme(
bodyText2: TextStyle(
color: Colors.white
)
)
),
initialRoute: Home.id,
routes: {
Home.id: (context) => Home()
},
);
}
}
现在我正在尝试将此容器内的所有文本小部件设置为黑色,但它仍在获取 Material 应用小部件中设置的白色。
class NewTask extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
Navigator.pop(context);
},
child: Theme(
data: ThemeData(
textTheme: TextTheme(
bodyText2: TextStyle(
color: Colors.black,
)
)
),
child: Container(
child: Column(
children: [
Text(
"New Task",
)
],
),
),
)
);
}
}
我能够通过将 Container 小部件包装在 Builder 方法中并设置来使其工作
TextStyle(
color: Theme.of(context).textTheme.bodyText2.color
),
但是如果我在这个容器中有多个文本小部件,这似乎需要做很多工作,我不明白出了什么问题,或者这不是主题小部件应该如何工作。
谢谢
【问题讨论】:
标签: flutter material-design themes