【问题标题】:Change the app theme at runtime在运行时更改应用主题
【发布时间】:2018-04-23 07:39:07
【问题描述】:

我是 Flutter 的初学者。 如何将整个应用程序主题从浅色主题更改为深色主题?

假设我有以下 Light Theme 的代码:

final ThemeData dTheme = new ThemeData(
  primarySwatch: Colors.orange,
  primaryColor: Colors.orangeAccent[100],
  primaryColorBrightness: Brightness.light,
);

和黑暗主题:

final ThemeData lTheme = new ThemeData(
  primarySwatch: Colors.indigo,
  primaryColor: Colors.indigoAccent[100],
  primaryColorBrightness: Brightness.dark,
);

我想在运行时点击按钮更改主题。

【问题讨论】:

    标签: android dart flutter


    【解决方案1】:

    这是此处回答的问题的具体案例:Force Flutter to redraw all widgets

    看看那个问题中提到的股票样本,特别注意: https://github.com/flutter/flutter/blob/master/examples/stocks/lib/main.dart https://github.com/flutter/flutter/blob/master/examples/stocks/lib/stock_settings.dart

    注意以下几点:

    1. 主题由_configuration指定,由configurationUpdater更新
    2. configurationUpdater 传递给需要它的应用的子级
    3. 孩子们可以调用该 configurationUpdater,这反过来会在应用的根目录设置状态,进而使用指定的主题重绘应用

    以下代码可能对您有所帮助:

    // How to use: Any Widget in the app can access the ThemeChanger
    // because it is an InheritedWidget. Then the Widget can call
    // themeChanger.theme = [blah] to change the theme. The ThemeChanger
    // then accesses AppThemeState by using the _themeGlobalKey, and
    // the ThemeChanger switches out the old ThemeData for the new
    // ThemeData in the AppThemeState (which causes a re-render).
    
    final _themeGlobalKey = new GlobalKey(debugLabel: 'app_theme');
    
    class AppTheme extends StatefulWidget {
    
      final child;
    
      AppTheme({
        this.child,
      }) : super(key: _themeGlobalKey);
    
      @override
      AppThemeState createState() => new AppThemeState();
    }
    
    class AppThemeState extends State<AppTheme> {
    
      ThemeData _theme = DEV_THEME;
    
      set theme(newTheme) {
        if (newTheme != _theme) {
          setState(() => _theme = newTheme);
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return new ThemeChanger(
          appThemeKey: _themeGlobalKey,
          child: new Theme(
            data: _theme,
            child: widget.child,
          ),
        );
      }
    }
    
    class ThemeChanger extends InheritedWidget {
    
      static ThemeChanger of(BuildContext context) {
        return context.inheritFromWidgetOfExactType(ThemeChanger);
      }
    
      final ThemeData theme;
      final GlobalKey _appThemeKey;
    
      ThemeChanger({
        appThemeKey,
        this.theme,
        child
      }) : _appThemeKey = appThemeKey, super(child: child);
    
      set appTheme(AppThemeOption theme) {
        switch (theme) {
          case AppThemeOption.experimental:
            (_appThemeKey.currentState as AppThemeState)?.theme = EXPERIMENT_THEME;
            break;
          case AppThemeOption.dev:
            (_appThemeKey.currentState as AppThemeState)?.theme = DEV_THEME;
            break;
        }
      }
    
      @override
      bool updateShouldNotify(ThemeChanger oldWidget) {
        return oldWidget.theme == theme;
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多