Flutter MobX更改道具更改主题

问题描述 投票:0回答:1

我正在使用MobX作为状态管理器的简单Flutter应用程序上工作。这个程序有两个主要主题:

  1. 暗模式
  2. 灯光模式

我希望我的应用在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会自行更新。有什么主意吗?

flutter material-design mobx android-darkmode
1个回答
0
投票

您应该使用来自mobx库的Observer窗口小部件到MaterialApp小部件上,并且changeThemeModeFunction将触发AppStore类中的@action函数

更多信息in the documentation

© www.soinside.com 2019 - 2024. All rights reserved.