From c0469ceaae699113fb1e84ab09cea914d171cd7c Mon Sep 17 00:00:00 2001 From: Predidit <34627277+Predidit@users.noreply.github.com> Date: Mon, 25 Mar 2024 13:34:09 +0800 Subject: [PATCH] add dark mode --- lib/app_widget.dart | 31 +++-- lib/pages/init_page.dart | 5 +- lib/pages/my/my_page.dart | 107 +++++++++++++++--- lib/utils/storage.dart | 1 + macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 64 +++++++++++ pubspec.yaml | 1 + 7 files changed, 176 insertions(+), 35 deletions(-) diff --git a/lib/app_widget.dart b/lib/app_widget.dart index 5ce76f0..87001d7 100644 --- a/lib/app_widget.dart +++ b/lib/app_widget.dart @@ -2,6 +2,7 @@ import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_modular/flutter_modular.dart'; +import 'package:adaptive_theme/adaptive_theme.dart'; class AppWidget extends StatefulWidget { const AppWidget({super.key}); @@ -13,23 +14,21 @@ class AppWidget extends StatefulWidget { class _AppWidgetState extends State { @override Widget build(BuildContext context) { - var app = MaterialApp.router( - title: "oneAnime", - localizationsDelegates: GlobalMaterialLocalizations.delegates, - supportedLocales: const [Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: "CN")], - locale: const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: "CN"), - theme: ThemeData( - useMaterial3: true, - primarySwatch: Colors.purple, + var app = AdaptiveTheme( + light: ThemeData.light(useMaterial3: true), + dark: ThemeData.dark(useMaterial3: true), + initial: AdaptiveThemeMode.system, + builder: (theme, darkTheme) => MaterialApp.router( + title: "oneAnime", + localizationsDelegates: GlobalMaterialLocalizations.delegates, + supportedLocales: const [Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: "CN")], + locale: const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: "CN"), + theme: theme, + darkTheme: darkTheme, + routerConfig: Modular.routerConfig, + builder: FlutterSmartDialog.init(), + // navigatorObservers: [Asuka.asukaHeroController], ), - darkTheme: ThemeData( - useMaterial3: true, - brightness: Brightness.dark, - primaryColor: Colors.purple, - ), - routerConfig: Modular.routerConfig, - builder: FlutterSmartDialog.init(), - // navigatorObservers: [Asuka.asukaHeroController], ); Modular.setObservers([FlutterSmartDialog.observer]); return app; diff --git a/lib/pages/init_page.dart b/lib/pages/init_page.dart index eefb473..af4e16f 100644 --- a/lib/pages/init_page.dart +++ b/lib/pages/init_page.dart @@ -3,16 +3,15 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_modular/flutter_modular.dart'; - - class InitPage extends StatefulWidget { const InitPage({super.key}); @override State createState() => _InitPageState(); -} +} class _InitPageState extends State { + // Box setting = GStorage.setting; @override void initState() { diff --git a/lib/pages/my/my_page.dart b/lib/pages/my/my_page.dart index 8163b19..daf377b 100644 --- a/lib/pages/my/my_page.dart +++ b/lib/pages/my/my_page.dart @@ -11,6 +11,7 @@ import 'package:oneanime/utils/storage.dart'; import 'package:oneanime/pages/my/my_controller.dart'; import 'package:hive/hive.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; +import 'package:adaptive_theme/adaptive_theme.dart'; class MyPage extends StatefulWidget { const MyPage({super.key}); @@ -23,6 +24,7 @@ class _MyPageState extends State { dynamic navigationBarState; Box setting = GStorage.setting; late dynamic defaultDanmakuArea; + late dynamic defaultThemeMode; final _mineController = Modular.get(); @override @@ -30,6 +32,8 @@ class _MyPageState extends State { super.initState(); defaultDanmakuArea = setting.get(SettingBoxKey.danmakuArea, defaultValue: 1.0); + defaultThemeMode = + setting.get(SettingBoxKey.themeMode, defaultValue: 'system'); WidgetsBinding.instance.addPostFrameCallback((_) { // 在widget构建完成后调用的函数 navigationBarState = Platform.isWindows @@ -52,6 +56,22 @@ class _MyPageState extends State { }); } + void updateTheme(String theme) async { + if (theme == 'dark') { + AdaptiveTheme.of(context).setDark(); + } + if (theme == 'light') { + AdaptiveTheme.of(context).setLight(); + } + if (theme == 'system') { + AdaptiveTheme.of(context).setSystem(); + } + await setting.put(SettingBoxKey.themeMode, theme); + setState(() { + defaultThemeMode = theme; + }); + } + @override Widget build(BuildContext context) { // This method is rerun every time setState is called, for instance as done @@ -101,22 +121,6 @@ class _MyPageState extends State { ), ListTile( onTap: () async { - // double? result = await showDialog( - // context: context, - // builder: (context) { - // return SelectDialog( - // title: '弹幕区域', - // value: defaultDanmakuArea, - // values: [0.25, 0.5, 1.0].map((e) { - // return {'title': '$e 屏幕', 'value': e}; - // }).toList()); - // }, - // ); - // if (result != null) { - // defaultDanmakuArea = result; - // setting.put(SettingBoxKey.danmakuArea, result); - // setState(() {}); - // } final List danAreaList = [ 0.25, 0.5, @@ -191,6 +195,77 @@ class _MyPageState extends State { color: Theme.of(context).colorScheme.primary, )), ), + ListTile( + onTap: () { + SmartDialog.show( + useAnimation: false, + builder: (context) { + return AlertDialog( + title: const Text('主题模式'), + content: StatefulBuilder( + builder: + (BuildContext context, StateSetter setState) { + return Wrap( + spacing: 8, + runSpacing: 2, + children: [ + defaultThemeMode == 'system' + ? FilledButton( + onPressed: () { + updateTheme('system'); + SmartDialog.dismiss(); + }, + child: const Text("跟随系统")) + : FilledButton.tonal( + onPressed: () { + updateTheme('system'); + SmartDialog.dismiss(); + }, + child: const Text("跟随系统")), + defaultThemeMode == 'light' + ? FilledButton( + onPressed: () { + updateTheme('light'); + SmartDialog.dismiss(); + }, + child: const Text("浅色")) + : FilledButton.tonal( + onPressed: () { + updateTheme('light'); + SmartDialog.dismiss(); + }, + child: const Text("浅色")), + defaultThemeMode == 'dark' + ? FilledButton( + onPressed: () { + updateTheme('dark'); + SmartDialog.dismiss(); + }, + child: const Text("深色")) + : FilledButton.tonal( + onPressed: () { + updateTheme('dark'); + SmartDialog.dismiss(); + }, + child: const Text("深色")), + ], + ); + }, + ), + ); + }); + }, + dense: false, + title: const Text('主题模式'), + subtitle: Text( + defaultThemeMode == 'light' + ? '浅色' + : (defaultThemeMode == 'dark' ? '深色' : '跟随系统'), + style: Theme.of(context) + .textTheme + .labelMedium! + .copyWith(color: Theme.of(context).colorScheme.outline)), + ), (Platform.isAndroid || Platform.isAndroid) ? const InkWell( child: SetSwitchItem( diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart index f59a109..fd27583 100644 --- a/lib/utils/storage.dart +++ b/lib/utils/storage.dart @@ -62,5 +62,6 @@ class SettingBoxKey { danmakuScroll = 'danmakuScroll', danmakuBottom = 'danmakuBottom', danmakuArea = 'danmakuArea', + themeMode = 'themeMode', autoPlay = 'autoPlay'; } diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 752749d..8e0ba39 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -14,6 +14,7 @@ import package_info_plus import path_provider_foundation import screen_brightness_macos import screen_retriever +import shared_preferences_foundation import sqflite import url_launcher_macos import wakelock_plus @@ -29,6 +30,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) ScreenBrightnessMacosPlugin.register(with: registry.registrar(forPlugin: "ScreenBrightnessMacosPlugin")) ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin")) + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin")) UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin")) WakelockPlusMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockPlusMacosPlugin")) diff --git a/pubspec.lock b/pubspec.lock index 5d5d283..e0ad22f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -9,6 +9,14 @@ packages: url: "https://pub.dev" source: hosted version: "67.0.0" + adaptive_theme: + dependency: "direct main" + description: + name: adaptive_theme + sha256: f4ee609b464e5efc68131d9d15ba9aa1de4e3b5ede64be17781c6e19a52d637d + url: "https://pub.dev" + source: hosted + version: "3.6.0" analyzer: dependency: transitive description: @@ -982,6 +990,62 @@ packages: url: "https://pub.dev" source: hosted version: "0.1.9" + shared_preferences: + dependency: transitive + description: + name: shared_preferences + sha256: "81429e4481e1ccfb51ede496e916348668fd0921627779233bd24cc3ff6abd02" + url: "https://pub.dev" + source: hosted + version: "2.2.2" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "8568a389334b6e83415b6aae55378e158fbc2314e074983362d20c562780fb06" + url: "https://pub.dev" + source: hosted + version: "2.2.1" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "7708d83064f38060c7b39db12aefe449cb8cdc031d6062280087bc4cdb988f5c" + url: "https://pub.dev" + source: hosted + version: "2.3.5" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: "7b15ffb9387ea3e237bb7a66b8a23d2147663d391cafc5c8f37b2e7b4bde5d21" + url: "https://pub.dev" + source: hosted + version: "2.2.2" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59" + url: "https://pub.dev" + source: hosted + version: "2.3.2" shelf: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 7772839..4908c8a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -71,6 +71,7 @@ dependencies: flutter_volume_controller: ^1.3.1 screen_brightness: ^0.2.2+1 wakelock_plus: ^1.1.1 + adaptive_theme: ^3.6.0 dev_dependencies: flutter_test: