我正在使用MobX作为状态管理器的简单Flutter应用程序上工作。这个程序有两个主要主题:
我希望我的应用在MobX存储状态更改时在运行时在这两个主题之间切换。这是我的main.dart:
void main() => runApp(MyApp());
final appStore = new AppStore();
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'my app',
theme: getThemeData(appStore.isDarkMode),
home: MyHomePage(appStore: appStore),
);
}
getThemeData(bool isDarkMode) {
if (isDarkMode) {
return ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.blue,
fontFamily: 'Sumsung-sharp-sans');
} else {
return ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.blue,
fontFamily: 'Sumsung-sharp-sans');
}
}
}
我希望当用户更改appStore.isDarkMode
时,整个应用程序主题都会更新并做出反应,但仅在应用程序渲染第一次更改时才会更改,而在商店更改时dnt会自行更新。有什么主意吗?
您应该使用来自mobx库的Observer窗口小部件到MaterialApp
小部件上,并且changeThemeModeFunction
将触发AppStore类中的@action
函数