我有2 ThemeData
个vars和SwitchListTile
,代码如下:
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,
);
当我更改开关状态时,如何使用新的ThemeData
重绘应用程序?
答案 0 :(得分:1)
您可能需要考虑将MaterialApp
嵌套在StatefulWidget
Flutter Gallery示例应用程序在GalleryApp小部件中执行此操作。
GalleryTheme _galleryTheme
实例变量以下是修改后的代码段
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,
);
}
}