【问题标题】:How to save a theme using a package Shared_Preferences如何使用包 Shared_Preferences 保存主题
【发布时间】:2021-12-30 19:18:34
【问题描述】:

帮我弄清楚在代码中的什么地方写一些东西,以便在重新启动应用程序后保存主题... 安装了animated_theme_switcher 包,我不知道在哪里开处方才能保留主题

我的代码:

@override
Widget build(BuildContext context) {
final isPlatformDark = WidgetsBinding.instance!.window.platformBrightness == Brightness.dark;
final theme = isPlatformDark ? darkTheme : lightTheme;

child: ThemeProvider(
        initTheme: theme,
        builder: (context, theme) {
          return MaterialApp(
            theme: theme,
            home: MyApp1(),
            debugShowCheckedModeBanner: false,
          );
        },
      ),

以及按钮上的操作

                                       onPressed: () async {
                                          var brightness =
                                              ThemeProvider.of(context)!.brightness;
                                          ThemeSwitcher.of(context)!.changeTheme(
                                            theme: brightness == Brightness.light
                                                ? darkTheme
                                                : lightTheme,
                                            reverseAnimation:
                                            brightness == Brightness.dark
                                                ? true
                                                : false,
                                          );
                                          setState(() {
                                            toggle = !toggle;
                                          });
                                        },

【问题讨论】:

标签: android flutter sharedpreferences


【解决方案1】:

尝试使用adaptive_theme, 自适应主题:https://pub.dev/packages/adaptive_theme

【讨论】:

    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多