繁体   English   中英

Flutter GetX主题模式问题

[英]Flutter GetX Theme Mode Issue

我目前正在开发一个实现了暗模式的应用程序。 当我将它从浅色模式切换到深色模式时,一切正常。 当我从暗模式切换回亮模式时,问题就开始了。 我在应用程序中添加的所有原色和主题配置都丢失了。

这是我的应用程序启动时的代码:


class TradeWixApp extends StatelessWidget {
  bool darkMode;
  TradeWixApp({Key? key, required this.darkMode}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      translations: Languages(),
      locale: Locale('en'),
      debugShowCheckedModeBanner: false,
      darkTheme: ThemeData(
        textTheme: TextTheme(
          titleMedium: TextStyle(color: Colors.white70),
          bodyMedium: TextStyle(color: Colors.white70),
        ),
        shadowColor: Colors.grey.shade900,
        brightness: Brightness.dark,
        backgroundColor: Colors.black,
        scaffoldBackgroundColor: Color.fromARGB(255, 37, 36, 36),
        appBarTheme: AppBarTheme(
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only(
            bottomLeft: Radius.circular(20),
            bottomRight: Radius.circular(20),
          )),
          color: Color.fromARGB(200, 174, 242, 135),
          elevation: 0,
          foregroundColor: Colors.black,
        ),
        primaryColor: Color.fromARGB(200, 174, 242, 135),
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            primary: Colors.black,
            elevation: 0,
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
          ),
        ),
        textButtonTheme: TextButtonThemeData(
          style: TextButton.styleFrom(primary: Colors.grey),
        ),
      ),
      themeMode: darkMode ? ThemeMode.dark : ThemeMode.light,
      theme: ThemeData(
          backgroundColor: Colors.white,
          scaffoldBackgroundColor: Colors.white,
          appBarTheme: AppBarTheme(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(20),
              bottomRight: Radius.circular(20),
            )),
            color: Color.fromARGB(255, 174, 242, 135),
            elevation: 0,
            foregroundColor: Colors.black,
          ),
          primaryColor: Color.fromARGB(255, 174, 242, 135),
          elevatedButtonTheme: ElevatedButtonThemeData(
            style: ElevatedButton.styleFrom(
              primary: Colors.black,
              elevation: 0,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20)),
            ),
          ),
          textButtonTheme: TextButtonThemeData(
            style: TextButton.styleFrom(primary: Colors.grey),
          )),
      home: const SplashScreen(),
    );
  }
}

我切换暗模式的代码是:

Obx(() {
                  return ListTile(
                      title: Text('Dark Mode'.tr),
                      trailing: Switch(
                        value: darkMode.value,
                        onChanged: (value) async {
                          darkMode.value = value;

                          Get.changeTheme(
                              !value ? ThemeData.light() : ThemeData.dark());
                          Get.changeThemeMode(
                              !value ? ThemeMode.light : ThemeMode.dark);
                          Get.reloadAll();
                          final prefs = await SharedPreferences.getInstance();
                          await prefs.setBool('darkMode', value);
                        },
                      ));
                })

这是切换前的灯这是切换前的灯

这是深色模式这是深色模式 这是切换后的灯光模式这是切换后的灯光模式

我不明白你为什么在你的代码中使用Get.changeTheme ,你已经在你的GetMaterialApp上设置了一个themedarkTheme ,所以要在它们之间切换,你只需要调用Get.changeThemeMode ,但是这个:

Get.changeTheme( !value ? ThemeData.light() : ThemeData.dark());

ThemeData.dark()ThemeData.light()是新的整个主题,我猜你的目标是在亮模式和暗模式之间切换,而不是在主题列表之间设置主题。 所以当你调用Get.changeTheme()时,你将你的应用程序主题重置为默认的浅色主题(这给了你主题被重置的行为)

尝试添加

themeMode: ThemeMode.system,

到您的 GetMaterialApp。

暂无
暂无

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

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