Skip to content

Commit

Permalink
提取主题到单独类中 #38
Browse files Browse the repository at this point in the history
  • Loading branch information
Muska-Ami committed Jan 14, 2024
1 parent 212591c commit 59389b4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
2 changes: 2 additions & 0 deletions lib/controller/dsettinglauncher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -57,5 +58,6 @@ class DSettingLauncherController extends GetxController {
theme_dark.value = value;
SettingStorage.save(await LauncherSettingPrefs.getInfo());
loadx();
// ThemeControl.switchDarkTheme(value);
}
}
38 changes: 5 additions & 33 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion lib/ui/setting/launcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -52,6 +53,7 @@ class LauncherSX {
SettingStorage.save(
await LauncherSettingPrefs.getInfo());
ds_c.loadx();
// ThemeControl.autoSet();
},
),
],
Expand Down Expand Up @@ -79,7 +81,7 @@ class LauncherSX {
),
Switch(
value: ds_c.theme_light_seed_enable.value,
onChanged: null),
onChanged: null,),
],
),
],
Expand Down
47 changes: 47 additions & 0 deletions lib/util/ThemeControl.dart
Original file line number Diff line number Diff line change
@@ -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,
),
);

}

0 comments on commit 59389b4

Please sign in to comment.