From 59389b4037aa23cab4e1c5effaea8362757e102f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=B2=AB=E8=8A=B1=E7=81=ABzzz?= Date: Sun, 14 Jan 2024 23:47:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8F=96=E4=B8=BB=E9=A2=98=E5=88=B0?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E7=B1=BB=E4=B8=AD=20#38?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/controller/dsettinglauncher.dart | 2 ++ lib/main.dart | 38 +++------------------- lib/ui/setting/launcher.dart | 4 ++- lib/util/ThemeControl.dart | 47 ++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 lib/util/ThemeControl.dart diff --git a/lib/controller/dsettinglauncher.dart b/lib/controller/dsettinglauncher.dart index 35f75252..1f35a149 100644 --- a/lib/controller/dsettinglauncher.dart +++ b/lib/controller/dsettinglauncher.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:nyalcf/io/settingStorage.dart'; import 'package:nyalcf/prefs/LauncherSettingPrefs.dart'; +import 'package:nyalcf/util/ThemeControl.dart'; import 'package:package_info_plus/package_info_plus.dart'; class DSettingLauncherController extends GetxController { @@ -57,5 +58,6 @@ class DSettingLauncherController extends GetxController { theme_dark.value = value; SettingStorage.save(await LauncherSettingPrefs.getInfo()); loadx(); + // ThemeControl.switchDarkTheme(value); } } diff --git a/lib/main.dart b/lib/main.dart index d5102b70..41ea008e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -15,6 +15,7 @@ import 'package:nyalcf/ui/panel/console.dart'; import 'package:nyalcf/ui/panel/home.dart'; import 'package:nyalcf/ui/panel/proxies.dart'; import 'package:nyalcf/ui/setting/injector.dart'; +import 'package:nyalcf/util/ThemeControl.dart'; Setting? _settings = null; @@ -55,47 +56,18 @@ class App extends StatelessWidget { ThemeData _theme_data; - final bool isDarkMode = (Theme.of(context).brightness == Brightness.dark || - Theme.of(context).colorScheme.brightness == Brightness.dark) || + final bool isDarkMode = SchedulerBinding.instance.platformDispatcher.platformBrightness == - Brightness.dark || - MediaQuery.of(context).platformBrightness == Brightness.dark; + Brightness.dark; print('System dark mode: ${isDarkMode}'); /// 判定是否需要切换暗色主题 if (((_settings?.theme_auto ?? true) && isDarkMode) || (_settings?.theme_dark ?? false)) { - _theme_data = ThemeData( - useMaterial3: true, - fontFamily: 'HarmonyOS Sans', - brightness: Brightness.dark, - ); - } else if ((_settings?.theme_light_seed_enable ?? false)) { - _theme_data = ThemeData( - useMaterial3: true, - fontFamily: 'HarmonyOS Sans', - colorScheme: ColorScheme.fromSeed( - seedColor: - Color('0x${_settings?.theme_light_seed ?? '66ccff'}' as int), - )); + _theme_data = ThemeControl.dark; } else { - _theme_data = ThemeData( - useMaterial3: true, - fontFamily: 'HarmonyOS Sans', - colorScheme: ColorScheme.fromSeed( - seedColor: Colors.pink.shade300, - ).copyWith( - primary: Colors.pink.shade400, - secondary: Colors.pink.shade300, - ), - appBarTheme: AppBarTheme( - color: Colors.pink.shade100, - ), - floatingActionButtonTheme: FloatingActionButtonThemeData( - backgroundColor: Colors.pink.shade200, - ), - ); + _theme_data = ThemeControl.light; } Get.put(UserController()); diff --git a/lib/ui/setting/launcher.dart b/lib/ui/setting/launcher.dart index 2acfbb7f..a04abfad 100644 --- a/lib/ui/setting/launcher.dart +++ b/lib/ui/setting/launcher.dart @@ -3,6 +3,7 @@ import 'package:get/get.dart'; import 'package:nyalcf/controller/dsettinglauncher.dart'; import 'package:nyalcf/io/settingStorage.dart'; import 'package:nyalcf/prefs/LauncherSettingPrefs.dart'; +import 'package:nyalcf/util/ThemeControl.dart'; import 'package:url_launcher/url_launcher.dart'; class LauncherSX { @@ -52,6 +53,7 @@ class LauncherSX { SettingStorage.save( await LauncherSettingPrefs.getInfo()); ds_c.loadx(); + // ThemeControl.autoSet(); }, ), ], @@ -79,7 +81,7 @@ class LauncherSX { ), Switch( value: ds_c.theme_light_seed_enable.value, - onChanged: null), + onChanged: null,), ], ), ], diff --git a/lib/util/ThemeControl.dart b/lib/util/ThemeControl.dart new file mode 100644 index 00000000..51d3388d --- /dev/null +++ b/lib/util/ThemeControl.dart @@ -0,0 +1,47 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:get/get.dart'; + +class ThemeControl { + + static void autoSet() { + final bool isDarkMode = + SchedulerBinding.instance.platformDispatcher.platformBrightness == + Brightness.dark; + switchDarkTheme(isDarkMode); + } + + static void switchDarkTheme(bool value) { + if (value) { + Get.changeTheme(dark); + print('Change to dark theme / ${value}'); + } else { + Get.changeTheme(light); + print('Change to light theme / ${value}'); + } + } + + static final dark = ThemeData( + useMaterial3: true, + fontFamily: 'HarmonyOS Sans', + brightness: Brightness.dark, + ); + static final light = ThemeData( + useMaterial3: true, + fontFamily: 'HarmonyOS Sans', + brightness: Brightness.light, + colorScheme: ColorScheme.fromSeed( + seedColor: Colors.pink.shade300, + ).copyWith( + primary: Colors.pink.shade400, + secondary: Colors.pink.shade300, + ), + appBarTheme: AppBarTheme( + color: Colors.pink.shade100, + ), + floatingActionButtonTheme: FloatingActionButtonThemeData( + backgroundColor: Colors.pink.shade200, + ), + ); + +} \ No newline at end of file