简体   繁体   English

如何使用存储在 sqflite FLUTTER 中的值在启动画面中设置 ThemeMode

[英]How to set ThemeMode in splash screen using value stored in sqflite FLUTTER

I have a Flutter Application where an sqflite database stored the user preference of ThemeMode (viz Dark, Light and System).我有一个 Flutter 应用程序,其中sqflite数据库存储了 ThemeMode 的用户首选项(即暗、亮和系统)。 I have created a splash screen using flutter_native_splash which supports dark mode too.我使用flutter_native_splash创建了一个启动画面,它也支持暗模式。
The Problem is this that I want the splash screen to follow the users stored value for theme mode.问题是我希望启动画面跟随用户为主题模式存储的值。 Currently, the code I am using is as follows:目前,我使用的代码如下:

class MyRoot extends StatefulWidget {
  // const MyRoot({Key? key}) : super(key: key);

  static ValueNotifier<ThemeMode> themeNotifier = ValueNotifier(ThemeMode.system);

  @override
  State<MyRoot> createState() => _MyRootState();
}


class _MyRootState extends State<MyRoot> {
  DatabaseHelper? databaseHelper = DatabaseHelper.dhInstance;
  ThemeMode? tmSaved;

  @override
  void initState() {
    Future.delayed(Duration.zero, () async => await loadData());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    //to prevent auto rotation of the app
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    return ValueListenableBuilder<ThemeMode>(
      valueListenable: MyRoot.themeNotifier,
      builder: (_, ThemeMode currentMode, __) {
        return Sizer(
          builder: (context, orientation, deviceType) {
            return MaterialApp(
              title: 'My Application',
              theme: themeLight, //dart file for theme
              darkTheme: themeDark, //dart file for theme
              themeMode: tmSaved ?? currentMode,
              initialRoute: // my initial root
              routes: {
                // my routes
                .
                .
                .
                // my routes
              },
            );
          },
        );
      },
    );
  }

  Future<void> loadData() async {
    if (databaseHelper != null) {
      ThemeMode? themeMode= await databaseHelper?.selectStoredTheme(); // function retrieving sqflite stored value and returning ThemeMode value
      if (themeMode != null) {
         MyRoot.themeNotifier.value = themeMode;
         return;
      }
    }
    MyRoot.themeNotifier.value = ThemeMode.system;
  }
}

Currently, this shows a light theme splash screen loading, then converts it into dark with a visible flicker.目前,这显示了一个浅色主题启动屏幕加载,然后将其转换为带有可见闪烁的深色。
ValueListenableBuilder<ThemeMode>(... is to enable real time theme change from settings page in my app which working as intended (taken from A Goodman's article: "Flutter: 2 Ways to Make a Dark/Light Mode Toggle" . ValueListenableBuilder<ThemeMode>(...是为了从我的应用程序中的设置页面启用实时主题更改,该页面按预期工作(取自古德曼的文章:“Flutter: 2 Ways to Make a Dark/Light Mode Toggle”

main.dart has the below code: main.dart代码如下:

void main() {
  runApp(MyRoot());
}

Have you tried loading the setting from sqflite in main() before runApp ?您是否尝试在sqflite之前从main()中的runApp加载设置? If you can manage to do so, you should be able to pass the setting as argument to MyRoot and then the widgets would be loaded from the start with the correct theme.如果你能做到这一点,你应该能够将设置作为参数传递给MyRoot ,然后小部件将从一开始就以正确的主题加载。 I'm speaking in theory, I can't test what I'm suggesting right now.我说的是理论上的,我现在无法测试我的建议。

Something like:就像是:

void main() async {
  ThemeMode? themeMode= await databaseHelper?.selectStoredTheme(); // function retrieving sqflite stored value and returning ThemeMode value
  runApp(MyRoot(themeMode));
}

[...]

class MyRoot extends StatefulWidget {
  ThemeMode? themeMode;

  const MyRoot(this.themeMode, {Key? key}) : super(key: key);

  static ValueNotifier<ThemeMode> themeNotifier = ValueNotifier(ThemeMode.system);

  @override
  State<MyRoot> createState() => _MyRootState();
}

Make your splashscreen.制作你的闪屏。 A main widget which get data from sqlflite And make splashscreen widget go to the your home widget with remove it using navigation pop-up一个从 sqlflite 获取数据并将启动屏幕小部件 go 制作到您的主页小部件的主小部件,并使用导航弹出窗口将其删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM