如何在Flutter中开始加载主题 [英] How to load theme at beginning in fluttter

查看:161
本文介绍了如何在Flutter中开始加载主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户可以更改主题颜色并将其保存在我的应用中.但是,我不知道在应用程序开始运行时如何加载已保存的主题颜色.例如,我想直接在下面的注释位置加载保存的主题颜色.我尝试了SharedPreference.但是,SharedPreference实例需要与 await 一起运行.似乎无法在这里使用.有什么方法可以直接在这里加载保存的主题,而不是使用setState之类的东西?

I want to users can change and save the theme color in my app. However, I have no ideas how to load the saved theme color when the app start running. For example, I want to load the saved theme color directly in the comment place below. I tried SharedPreference. However the SharedPreference instance need to run with await. It seems can't be used here. Is there any way I can load saved theme here directly instead of using setState or something like it?

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: // how to load saved theme here?
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

推荐答案

这个答案还要进一步.它显示了如何加载和保存主题首选项,如何构建ThemeData以及如何从应用程序页面更改主题.

This answer goes a bit further. It shows how to load and save theme preferences, how to build a ThemeData, and how to change the theme from a page of your app.

  • 使用shared_preferences插件保存用户首选项(选择了主题).
  • 使用整个Flutter框架中使用的控制器模式"将当前选定的主题(及其更改)提供给您的应用.
  • 使用InheritedWidget可以在应用程序的任何部分使用控制器.
  • Save the user preferences (which theme is selected) using the shared_preferences plugin.
  • Use the "controller pattern" that is used throughout the Flutter framework to provide the currently selected theme (and changes to it) to your app.
  • Use an InheritedWidget to use the controller in any part of your app.

这是控制器的外观:

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// provides the currently selected theme, saves changed theme preferences to disk
class ThemeController extends ChangeNotifier {
  static const themePrefKey = 'theme';

  ThemeController(this._prefs) {
    // load theme from preferences on initialization
    _currentTheme = _prefs.getString(themePrefKey) ?? 'light';
  }

  final SharedPreferences _prefs;
  String _currentTheme;

  /// get the current theme
  String get currentTheme => _currentTheme;

  void setTheme(String theme) {
    _currentTheme = theme;

    // notify the app that the theme was changed
    notifyListeners();

    // store updated theme on disk
    _prefs.setString(themePrefKey, theme);
  }

  /// get the controller from any page of your app
  static ThemeController of(BuildContext context) {
    final provider = context.inheritFromWidgetOfExactType(ThemeControllerProvider) as ThemeControllerProvider;
    return provider.controller;
  }
}

/// provides the theme controller to any page of your app
class ThemeControllerProvider extends InheritedWidget {
  const ThemeControllerProvider({Key key, this.controller, Widget child}) : super(key: key, child: child);

  final ThemeController controller;

  @override
  bool updateShouldNotify(ThemeControllerProvider old) => controller != old.controller;
}

这是您在应用中使用控制器和InheritedWidget的方式:

Here is how you would use the controller and InheritedWidget in your app:

void main() async {
  // load the shared preferences from disk before the app is started
  final prefs = await SharedPreferences.getInstance();

  // create new theme controller, which will get the currently selected from shared preferences
  final themeController = ThemeController(prefs);

  runApp(MyApp(themeController: themeController));
}

class MyApp extends StatelessWidget {
  final ThemeController themeController;

  const MyApp({Key key, this.themeController}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // use AnimatedBuilder to listen to theme changes (listen to ChangeNotifier)
    // the app will be rebuilt when the theme changes
    return AnimatedBuilder(
      animation: themeController,
      builder: (context, _) {
        // wrap app in inherited widget to provide the ThemeController to all pages
        return ThemeControllerProvider(
          controller: themeController,
          child: MaterialApp(
            title: 'Flutter Demo',
            theme: _buildCurrentTheme(),
            home: MyHomePage(),
          ),
        );
      },
    );
  }

  // build the flutter theme from the saved theme string
  ThemeData _buildCurrentTheme() {
    switch (themeController.currentTheme) {
      case "dark":
        return ThemeData(
          brightness: Brightness.dark,
          primarySwatch: Colors.orange,
        );
      case "light":
      default:
        return ThemeData(
          brightness: Brightness.light,
          primarySwatch: Colors.blue,
        );
    }
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                // thanks to the inherited widget, we can access the theme controller from any page
                ThemeController.of(context).setTheme('light');
              },
              child: Text('Light Theme'),
            ),
            RaisedButton(
              onPressed: () {
                ThemeController.of(context).setTheme('dark');
              },
              child: Text('Dark Theme'),
            )
          ],
        ),
      ),
    );
  }
}

这篇关于如何在Flutter中开始加载主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆