Dart 如何使用新的主题数据重新绘制整个应用程序?
我有2个Dart 如何使用新的主题数据重新绘制整个应用程序?,dart,flutter,Dart,Flutter,我有2个主题数据变量和一个开关列表磁贴,带有以下代码: new SwitchListTile( value: applyDarkTheme, title: const Text('Appy dark theme?'), onChanged: (bool value) { setState(() {
主题数据
变量和一个开关列表磁贴
,带有以下代码:
new SwitchListTile(
value: applyDarkTheme,
title: const Text('Appy dark theme?'),
onChanged: (bool value) {
setState(() {
applyDarkTheme = value;
});
})
applyDarkTheme
是一个变量,我仅在第一次创建应用程序时才检查它:
return new MaterialApp(
title: 'Test Application',
home: new MyHomePage(title: 'Test app'),
theme: settings.applyDarkTheme ? AppThemes.dark : AppThemes.light,
routes: _routes,
);
我如何在更改开关状态时用新的<代码>主题> /代码>重新绘制应用程序?
< p>您可能需要考虑在一个状态控件中嵌套您的<代码> ObjalApp < /C>。- 使用一个简单、轻量级的小部件类,例如
- 这将创建相应的类
- 此类具有
实例变量GalleryTheme\u GalleryTheme
- 您需要在方法中设置/更改此值
- 然后,您可以将它们添加到MaterialApp构造函数中
class GalleryApp extends StatefulWidget {
@override
GalleryAppState createState() => new GalleryAppState();
}
class GalleryAppState extends State<GalleryApp> {
GalleryTheme _galleryTheme = kAllGalleryThemes[0];
...
@override
Widget build(BuildContext context) {
Widget home = new GalleryHome(
galleryTheme: _galleryTheme,
onThemeChanged: (GalleryTheme value) {
setState(() {
_galleryTheme = value;
});
},
...
);
...
return new MaterialApp(
...
home: home,
);
}
}
class GalleryApp扩展StatefulWidget{
@凌驾
GalleryAppState createState()=>新GalleryAppState();
}
类GalleryAppState扩展了状态{
GalleryTheme _GalleryTheme=kAllGalleryThemes[0];
...
@凌驾
小部件构建(构建上下文){
小部件主页=新厨房主页(
厨房主题:_厨房主题,
onThemeChanged:(Gallery主题值){
设置状态(){
_厨房主题=价值;
});
},
...
);
...
返回新材料PP(
...
家:家,
);
}
}
< /代码> 您可能想考虑在一个状态窗口小部件中嵌套您的<代码> ObjalApp < /C>。
FlatterGallery示例应用程序在其小部件中执行此操作
- 使用一个简单、轻量级的小部件类,例如
- 这将创建相应的类
- 此类具有
GalleryTheme\u GalleryTheme
实例变量
- 您需要在方法中设置/更改此值
- 然后,您可以将它们添加到MaterialApp构造函数中
下面是一个修改过的代码段
class GalleryApp extends StatefulWidget {
@override
GalleryAppState createState() => new GalleryAppState();
}
class GalleryAppState extends State<GalleryApp> {
GalleryTheme _galleryTheme = kAllGalleryThemes[0];
...
@override
Widget build(BuildContext context) {
Widget home = new GalleryHome(
galleryTheme: _galleryTheme,
onThemeChanged: (GalleryTheme value) {
setState(() {
_galleryTheme = value;
});
},
...
);
...
return new MaterialApp(
...
home: home,
);
}
}
class GalleryApp扩展StatefulWidget{
@凌驾
GalleryAppState createState()=>新GalleryAppState();
}
类GalleryAppState扩展了状态{
GalleryTheme _GalleryTheme=kAllGalleryThemes[0];
...
@凌驾
小部件构建(构建上下文){
小部件主页=新厨房主页(
厨房主题:_厨房主题,
onThemeChanged:(Gallery主题值){
设置状态(){
_厨房主题=价值;
});
},
...
);
...
返回新材料PP(
...
家:家,
);
}
}
谢谢!经过多次编辑,我终于成功了。谢谢你!经过多次编辑,我终于成功了。