From 891f15cd5862a2328725df7431c09390eafa1f2a Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 20 Mar 2023 18:04:05 -0600 Subject: [PATCH] choose custom themes to set based on the system's light/dark brightness --- lib/hive/db.dart | 2 +- lib/main.dart | 86 ++-- .../appearance_settings_view.dart | 428 +++++++++++++++--- .../settings_menu/appearance_settings.dart | 15 +- lib/route_generator.dart | 16 + lib/utilities/constants.dart | 2 +- lib/utilities/db_version_migration.dart | 20 + lib/utilities/delete_everything.dart | 2 +- lib/utilities/prefs.dart | 50 +- lib/utilities/theme/color_theme.dart | 23 + 10 files changed, 506 insertions(+), 138 deletions(-) diff --git a/lib/hive/db.dart b/lib/hive/db.dart index 5c4f252e6..d56e5d243 100644 --- a/lib/hive/db.dart +++ b/lib/hive/db.dart @@ -31,7 +31,7 @@ class DB { static const String boxNameWalletsToDeleteOnStart = "walletsToDeleteOnStart"; static const String boxNamePriceCache = "priceAPIPrice24hCache"; static const String boxNameDBInfo = "dbInfo"; - static const String boxNameTheme = "theme"; + // static const String boxNameTheme = "theme"; static const String boxNameDesktopData = "desktopData"; static const String boxNameBuys = "buysBox"; diff --git a/lib/main.dart b/lib/main.dart index dc4b6877e..0c3a685d4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,7 +8,6 @@ import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; import 'package:flutter_libmonero/monero/monero.dart'; import 'package:flutter_libmonero/wownero/wownero.dart'; @@ -59,15 +58,7 @@ import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/prefs.dart'; import 'package:stackwallet/utilities/stack_file_system.dart'; -import 'package:stackwallet/utilities/theme/chan_colors.dart'; import 'package:stackwallet/utilities/theme/color_theme.dart'; -import 'package:stackwallet/utilities/theme/dark_colors.dart'; -import 'package:stackwallet/utilities/theme/forest_colors.dart'; -import 'package:stackwallet/utilities/theme/fruit_sorbet_colors.dart'; -import 'package:stackwallet/utilities/theme/light_colors.dart'; -import 'package:stackwallet/utilities/theme/ocean_breeze_colors.dart'; -import 'package:stackwallet/utilities/theme/oled_black_colors.dart'; -import 'package:stackwallet/utilities/theme/oled_chans_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/util.dart'; import 'package:window_size/window_size.dart'; @@ -190,8 +181,6 @@ void main() async { monero.onStartup(); wownero.onStartup(); - await Hive.openBox(DB.boxNameTheme); - // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, // overlays: [SystemUiOverlay.bottom]); await NotificationApi.init(); @@ -339,53 +328,27 @@ class _MaterialAppWithThemeState extends ConsumerState @override void initState() { - final String? colorScheme; + StackColorTheme colorTheme; if (ref.read(prefsChangeNotifierProvider).enableSystemBrightness) { - final brightness = - SchedulerBinding.instance.platformDispatcher.platformBrightness; + final brightness = WidgetsBinding.instance.window.platformBrightness; switch (brightness) { case Brightness.dark: - colorScheme = - ref.read(prefsChangeNotifierProvider).systemBrightnessDarkTheme; + colorTheme = ref + .read(prefsChangeNotifierProvider) + .systemBrightnessDarkTheme + .colorTheme; break; case Brightness.light: - colorScheme = - ref.read(prefsChangeNotifierProvider).systemBrightnessLightTheme; + colorTheme = ref + .read(prefsChangeNotifierProvider) + .systemBrightnessLightTheme + .colorTheme; break; } } else { - colorScheme = - DB.instance.get(boxName: DB.boxNameTheme, key: "colorScheme") - as String?; + colorTheme = ref.read(prefsChangeNotifierProvider).theme.colorTheme; } - StackColorTheme colorTheme; - switch (colorScheme) { - case "dark": - colorTheme = DarkColors(); - break; - case "oledBlack": - colorTheme = OledBlackColors(); - break; - case "oceanBreeze": - colorTheme = OceanBreezeColors(); - break; - case "fruitSorbet": - colorTheme = FruitSorbetColors(); - break; - case "forest": - colorTheme = ForestColors(); - break; - case "chan": - colorTheme = ChanColors(); - break; - case "oledChans": - colorTheme = DarkChansColors(); - break; - case "light": - default: - colorTheme = LightColors(); - } loadingCompleter = Completer(); WidgetsBinding.instance.addObserver(this); // load locale and prefs @@ -414,6 +377,33 @@ class _MaterialAppWithThemeState extends ConsumerState } }); + WidgetsBinding.instance.window.onPlatformBrightnessChanged = () { + StackColorTheme colorTheme; + switch (WidgetsBinding.instance.window.platformBrightness) { + case Brightness.dark: + colorTheme = ref + .read(prefsChangeNotifierProvider) + .systemBrightnessDarkTheme + .colorTheme; + break; + case Brightness.light: + colorTheme = ref + .read(prefsChangeNotifierProvider) + .systemBrightnessLightTheme + .colorTheme; + break; + } + + WidgetsBinding.instance.addPostFrameCallback((_) { + print("============================================================="); + print("colorTheme: $colorTheme"); + print("============================================================="); + + ref.read(colorThemeProvider.notifier).state = + StackColors.fromStackColorTheme(colorTheme); + }); + }; + super.initState(); } diff --git a/lib/pages/settings_views/global_settings_view/appearance_settings_view.dart b/lib/pages/settings_views/global_settings_view/appearance_settings_view.dart index 9f50402b0..981ddc69c 100644 --- a/lib/pages/settings_views/global_settings_view/appearance_settings_view.dart +++ b/lib/pages/settings_views/global_settings_view/appearance_settings_view.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:stackwallet/hive/db.dart'; +import 'package:flutter_svg/flutter_svg.dart'; import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/providers/ui/color_theme_provider.dart'; import 'package:stackwallet/utilities/assets.dart'; @@ -11,7 +11,10 @@ import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/custom_buttons/draggable_switch_button.dart'; +import 'package:stackwallet/widgets/expandable.dart'; +import 'package:stackwallet/widgets/rounded_container.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; +import 'package:tuple/tuple.dart'; class AppearanceSettingsView extends ConsumerWidget { const AppearanceSettingsView({Key? key}) : super(key: key); @@ -101,57 +104,7 @@ class AppearanceSettingsView extends ConsumerWidget { const SizedBox( height: 10, ), - RoundedWhiteContainer( - child: Consumer( - builder: (_, ref, __) { - return RawMaterialButton( - splashColor: Theme.of(context) - .extension()! - .highlight, - materialTapTargetSize: - MaterialTapTargetSize.shrinkWrap, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular( - Constants.size.circularBorderRadius, - ), - ), - onPressed: null, - child: Padding( - padding: - const EdgeInsets.symmetric(vertical: 8), - child: Row( - mainAxisAlignment: - MainAxisAlignment.spaceBetween, - children: [ - Text( - "System brightness", - style: STextStyles.titleBold12(context), - textAlign: TextAlign.left, - ), - SizedBox( - height: 20, - width: 40, - child: DraggableSwitchButton( - isOn: ref.watch( - prefsChangeNotifierProvider.select( - (value) => value - .enableSystemBrightness), - ), - onValueChanged: (newValue) { - ref - .read( - prefsChangeNotifierProvider) - .enableSystemBrightness = newValue; - }, - ), - ) - ], - ), - ), - ); - }, - ), - ), + const SystemBrightnessToggle(), if (!ref.watch( prefsChangeNotifierProvider .select((value) => value.enableSystemBrightness), @@ -214,6 +167,238 @@ class AppearanceSettingsView extends ConsumerWidget { } } +class SystemBrightnessToggle extends ConsumerStatefulWidget { + const SystemBrightnessToggle({Key? key}) : super(key: key); + + @override + ConsumerState createState() => + _SystemBrightnessToggleState(); +} + +class _SystemBrightnessToggleState + extends ConsumerState { + final controller = ExpandableController(); + + void _toggle(bool enable) { + ref.read(prefsChangeNotifierProvider).enableSystemBrightness = enable; + + if (enable && controller.state == ExpandableState.collapsed) { + controller.toggle?.call(); + } else if (!enable && controller.state == ExpandableState.expanded) { + controller.toggle?.call(); + } + + if (enable) { + final ThemeType type; + switch (MediaQuery.of(context).platformBrightness) { + case Brightness.dark: + type = ref + .read(prefsChangeNotifierProvider.notifier) + .systemBrightnessDarkTheme; + break; + case Brightness.light: + type = ref + .read(prefsChangeNotifierProvider.notifier) + .systemBrightnessLightTheme; + break; + } + ref.read(colorThemeProvider.notifier).state = + StackColors.fromStackColorTheme( + type.colorTheme, + ); + } else { + ref.read(prefsChangeNotifierProvider.notifier).theme = + ref.read(colorThemeProvider.notifier).state.themeType; + } + } + + @override + void initState() { + if (ref.read(prefsChangeNotifierProvider).enableSystemBrightness) { + WidgetsBinding.instance.addPostFrameCallback((_) { + controller.toggle?.call(); + }); + } + super.initState(); + } + + @override + Widget build(BuildContext context) { + final enable = ref.watch( + prefsChangeNotifierProvider + .select((value) => value.enableSystemBrightness), + ); + + return RoundedWhiteContainer( + child: Expandable( + controller: controller, + expandOverride: () { + _toggle( + !ref.read(prefsChangeNotifierProvider).enableSystemBrightness); + }, + header: RawMaterialButton( + splashColor: Theme.of(context).extension()!.highlight, + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular( + Constants.size.circularBorderRadius, + ), + ), + onPressed: null, + child: Padding( + padding: const EdgeInsets.symmetric(vertical: 8), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + "System brightness", + style: STextStyles.titleBold12(context), + textAlign: TextAlign.left, + ), + SizedBox( + key: Key("${enable}enableSystemBrightnessToggleKey"), + height: 20, + width: 40, + child: DraggableSwitchButton( + isOn: enable, + onValueChanged: _toggle, + ), + ) + ], + ), + ), + ), + body: Column( + children: [ + RoundedContainer( + color: Colors.transparent, + padding: EdgeInsets.zero, + onPressed: () async { + final result = await Navigator.of(context).pushNamed( + SystemBrightnessThemeSelectionView.routeName, + arguments: Tuple2( + "light", + ref + .read(prefsChangeNotifierProvider) + .systemBrightnessLightTheme), + ); + if (result is ThemeType) { + ref + .read(prefsChangeNotifierProvider) + .systemBrightnessLightTheme = result; + if (ref + .read(prefsChangeNotifierProvider) + .enableSystemBrightness) { + if (mounted && + MediaQuery.of(context).platformBrightness == + Brightness.light) { + ref.read(colorThemeProvider.notifier).state = + StackColors.fromStackColorTheme(result.colorTheme); + } + } + } + }, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Light theme", + style: STextStyles.itemSubtitle(context), + ), + const SizedBox( + height: 2, + ), + Text( + ref + .watch( + prefsChangeNotifierProvider.select((value) => + value.systemBrightnessLightTheme), + ) + .prettyName, + style: STextStyles.itemSubtitle12(context), + ), + ], + ), + ), + SvgPicture.asset( + Assets.svg.chevronRight, + color: Theme.of(context).extension()!.textDark, + ), + ], + ), + ), + RoundedContainer( + color: Colors.transparent, + padding: EdgeInsets.zero, + onPressed: () async { + final result = await Navigator.of(context).pushNamed( + SystemBrightnessThemeSelectionView.routeName, + arguments: Tuple2( + "dark", + ref + .read(prefsChangeNotifierProvider) + .systemBrightnessDarkTheme), + ); + if (result is ThemeType) { + ref + .read(prefsChangeNotifierProvider) + .systemBrightnessDarkTheme = result; + if (ref + .read(prefsChangeNotifierProvider) + .enableSystemBrightness) { + if (mounted && + MediaQuery.of(context).platformBrightness == + Brightness.dark) { + ref.read(colorThemeProvider.notifier).state = + StackColors.fromStackColorTheme(result.colorTheme); + } + } + } + }, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Dark theme", + style: STextStyles.itemSubtitle(context), + ), + const SizedBox( + height: 2, + ), + Text( + ref.watch( + prefsChangeNotifierProvider.select((value) => + value.systemBrightnessDarkTheme.prettyName), + ), + style: STextStyles.itemSubtitle12(context), + ), + ], + ), + ), + SvgPicture.asset( + Assets.svg.chevronRight, + color: Theme.of(context).extension()!.textDark, + ), + ], + ), + ) + ], + ), + ), + ); + } +} + class ThemeOptionsView extends ConsumerWidget { const ThemeOptionsView({Key? key}) : super(key: key); @@ -228,12 +413,9 @@ class ThemeOptionsView extends ConsumerWidget { ) : ThemeOption( onPressed: () { - DB.instance.put( - boxName: DB.boxNameTheme, - key: "colorScheme", - value: ThemeType.values[i ~/ 2].name, - ); - ref.read(colorThemeProvider.state).state = + ref.read(prefsChangeNotifierProvider.notifier).theme = + ThemeType.values[i ~/ 2]; + ref.read(colorThemeProvider.notifier).state = StackColors.fromStackColorTheme( ThemeType.values[i ~/ 2].colorTheme, ); @@ -241,12 +423,9 @@ class ThemeOptionsView extends ConsumerWidget { }, onChanged: (newValue) { if (newValue == ThemeType.values[i ~/ 2]) { - DB.instance.put( - boxName: DB.boxNameTheme, - key: "colorScheme", - value: ThemeType.values[i ~/ 2].name, - ); - ref.read(colorThemeProvider.state).state = + ref.read(prefsChangeNotifierProvider.notifier).theme = + ThemeType.values[i ~/ 2]; + ref.read(colorThemeProvider.notifier).state = StackColors.fromStackColorTheme( ThemeType.values[i ~/ 2].colorTheme, ); @@ -326,3 +505,124 @@ class ThemeOption extends StatelessWidget { ); } } + +class SystemBrightnessThemeSelectionView extends StatelessWidget { + const SystemBrightnessThemeSelectionView({ + Key? key, + required this.brightness, + required this.current, + }) : super(key: key); + + final String brightness; + final ThemeType current; + + static const String routeName = "/chooseSystemTheme"; + + @override + Widget build(BuildContext context) { + return Background( + child: Scaffold( + backgroundColor: Theme.of(context).extension()!.background, + appBar: AppBar( + leading: AppBarBackButton( + onPressed: () async { + Navigator.of(context).pop(); + }, + ), + title: Text( + "Choose $brightness theme", + style: STextStyles.navBarTitle(context), + ), + ), + body: Padding( + padding: const EdgeInsets.all(16), + child: LayoutBuilder( + builder: (context, constraints) { + return SingleChildScrollView( + child: ConstrainedBox( + constraints: BoxConstraints( + minHeight: constraints.maxHeight, + ), + child: IntrinsicHeight( + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + RoundedWhiteContainer( + padding: const EdgeInsets.all(0), + child: RawMaterialButton( + // splashColor: Theme.of(context).extension()!.highlight, + padding: const EdgeInsets.all(0), + materialTapTargetSize: + MaterialTapTargetSize.shrinkWrap, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular( + Constants.size.circularBorderRadius, + ), + ), + onPressed: null, + child: Padding( + padding: const EdgeInsets.all(12), + child: Row( + children: [ + Column( + crossAxisAlignment: + CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.all(10), + child: Column( + children: [ + for (int i = 0; + i < + (2 * + ThemeType.values + .length) - + 1; + i++) + (i % 2 == 1) + ? const SizedBox( + height: 10, + ) + : ThemeOption( + onPressed: () { + Navigator.of(context) + .pop(ThemeType + .values[ + i ~/ 2]); + }, + onChanged: (newValue) { + if (newValue == + ThemeType.values[ + i ~/ 2]) { + Navigator.of(context) + .pop(ThemeType + .values[ + i ~/ 2]); + } + }, + value: ThemeType + .values[i ~/ 2], + groupValue: current, + ), + ], + ), + ), + ], + ), + ], + ), + ), + ), + ), + ], + ), + ), + ), + ); + }, + ), + ), + ), + ); + } +} diff --git a/lib/pages_desktop_specific/settings/settings_menu/appearance_settings.dart b/lib/pages_desktop_specific/settings/settings_menu/appearance_settings.dart index 4452d0467..40a53a1dc 100644 --- a/lib/pages_desktop_specific/settings/settings_menu/appearance_settings.dart +++ b/lib/pages_desktop_specific/settings/settings_menu/appearance_settings.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; -import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/providers/ui/color_theme_provider.dart'; import 'package:stackwallet/utilities/assets.dart'; @@ -232,14 +231,12 @@ class _ThemeToggle extends ConsumerState { cursor: SystemMouseCursors.click, child: GestureDetector( onTap: () { - if (ref.read(colorThemeProvider.state).state.themeType != + if (ref.read(colorThemeProvider.notifier).state.themeType != ThemeType.values[i]) { - DB.instance.put( - boxName: DB.boxNameTheme, - key: "colorScheme", - value: ThemeType.values[i].name, - ); - ref.read(colorThemeProvider.state).state = + ref.read(prefsChangeNotifierProvider.notifier).theme = + ThemeType.values[i]; + + ref.read(colorThemeProvider.notifier).state = StackColors.fromStackColorTheme( ThemeType.values[i].colorTheme); } @@ -255,7 +252,7 @@ class _ThemeToggle extends ConsumerState { border: Border.all( width: 2.5, color: ref - .read(colorThemeProvider.state) + .read(colorThemeProvider.notifier) .state .themeType == ThemeType.values[i] diff --git a/lib/route_generator.dart b/lib/route_generator.dart index 64fd1e010..9741885a1 100644 --- a/lib/route_generator.dart +++ b/lib/route_generator.dart @@ -133,6 +133,7 @@ import 'package:stackwallet/services/event_bus/events/global/node_connection_sta import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart'; import 'package:stackwallet/utilities/enums/add_wallet_type_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; +import 'package:stackwallet/utilities/theme/color_theme.dart'; import 'package:tuple/tuple.dart'; class RouteGenerator { @@ -640,6 +641,21 @@ class RouteGenerator { } return _routeError("${settings.name} invalid args: ${args.toString()}"); + case SystemBrightnessThemeSelectionView.routeName: + if (args is Tuple2) { + return getRoute( + shouldUseMaterialRoute: useMaterialPageRoute, + builder: (_) => SystemBrightnessThemeSelectionView( + brightness: args.item1, + current: args.item2, + ), + settings: RouteSettings( + name: settings.name, + ), + ); + } + return _routeError("${settings.name} invalid args: ${args.toString()}"); + case WalletNetworkSettingsView.routeName: if (args is Tuple3) { return getRoute( diff --git a/lib/utilities/constants.dart b/lib/utilities/constants.dart index 19516c5b9..9f1eaa4c7 100644 --- a/lib/utilities/constants.dart +++ b/lib/utilities/constants.dart @@ -41,7 +41,7 @@ abstract class Constants { // Enable Logger.print statements static const bool disableLogger = false; - static const int currentHiveDbVersion = 5; + static const int currentHiveDbVersion = 6; static const int rescanV1 = 1; diff --git a/lib/utilities/db_version_migration.dart b/lib/utilities/db_version_migration.dart index 67e76ef29..c732ee9c2 100644 --- a/lib/utilities/db_version_migration.dart +++ b/lib/utilities/db_version_migration.dart @@ -185,6 +185,26 @@ class DbVersionMigrator with WalletDB { // try to continue migrating return await migrate(5, secureStore: secureStore); + case 5: + // migrate + await Hive.openBox("theme"); + await Hive.openBox(DB.boxNamePrefs); + + final themeName = + DB.instance.get(boxName: "theme", key: "colorScheme") + as String? ?? + "light"; + + await DB.instance.put( + boxName: DB.boxNamePrefs, key: "theme", value: themeName); + + // update version + await DB.instance.put( + boxName: DB.boxNameDBInfo, key: "hive_data_version", value: 6); + + // try to continue migrating + return await migrate(6, secureStore: secureStore); + default: // finally return return; diff --git a/lib/utilities/delete_everything.dart b/lib/utilities/delete_everything.dart index 497cd71f4..f9a33dbaf 100644 --- a/lib/utilities/delete_everything.dart +++ b/lib/utilities/delete_everything.dart @@ -21,7 +21,7 @@ Future deleteEverything() async { .deleteBoxFromDisk(boxName: DB.boxNameWalletsToDeleteOnStart); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePriceCache); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameDBInfo); - await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTheme); + await DB.instance.deleteBoxFromDisk(boxName: "theme"); return true; } catch (e, s) { Logging.instance.log("$e $s", level: LogLevel.Error); diff --git a/lib/utilities/prefs.dart b/lib/utilities/prefs.dart index 78551a695..25388c6ff 100644 --- a/lib/utilities/prefs.dart +++ b/lib/utilities/prefs.dart @@ -43,6 +43,7 @@ class Prefs extends ChangeNotifier { _signupEpoch = await _getSignupEpoch(); _enableCoinControl = await _getEnableCoinControl(); _enableSystemBrightness = await _getEnableSystemBrightness(); + _theme = await _getTheme(); _systemBrightnessLightTheme = await _getSystemBrightnessLightTheme(); _systemBrightnessDarkTheme = await _getSystemBrightnessDarkTheme(); @@ -699,49 +700,70 @@ class Prefs extends ChangeNotifier { // system brightness light theme name - String _systemBrightnessLightTheme = ThemeType.light.name; + ThemeType _theme = ThemeType.light; - String get systemBrightnessLightTheme => _systemBrightnessLightTheme; + ThemeType get theme => _theme; - set systemBrightnessLightTheme(String systemBrightnessLightTheme) { + set theme(ThemeType theme) { + if (this.theme != theme) { + DB.instance.put( + boxName: DB.boxNamePrefs, key: "theme", value: theme.name); + _theme = theme; + notifyListeners(); + } + } + + Future _getTheme() async { + return ThemeTypeExt.fromName(await DB.instance + .get(boxName: DB.boxNamePrefs, key: "theme") as String? ?? + ThemeType.light.name); + } + + // system brightness light theme name + + ThemeType _systemBrightnessLightTheme = ThemeType.light; + + ThemeType get systemBrightnessLightTheme => _systemBrightnessLightTheme; + + set systemBrightnessLightTheme(ThemeType systemBrightnessLightTheme) { if (this.systemBrightnessLightTheme != systemBrightnessLightTheme) { DB.instance.put( boxName: DB.boxNamePrefs, key: "systemBrightnessLightTheme", - value: systemBrightnessLightTheme); + value: systemBrightnessLightTheme.name); _systemBrightnessLightTheme = systemBrightnessLightTheme; notifyListeners(); } } - Future _getSystemBrightnessLightTheme() async { - return await DB.instance.get( + Future _getSystemBrightnessLightTheme() async { + return ThemeTypeExt.fromName(await DB.instance.get( boxName: DB.boxNamePrefs, key: "systemBrightnessLightTheme") as String? ?? - ThemeType.light.name; + ThemeType.light.name); } // system brightness dark theme name - String _systemBrightnessDarkTheme = ThemeType.dark.name; + ThemeType _systemBrightnessDarkTheme = ThemeType.dark; - String get systemBrightnessDarkTheme => _systemBrightnessDarkTheme; + ThemeType get systemBrightnessDarkTheme => _systemBrightnessDarkTheme; - set systemBrightnessDarkTheme(String systemBrightnessDarkTheme) { + set systemBrightnessDarkTheme(ThemeType systemBrightnessDarkTheme) { if (this.systemBrightnessDarkTheme != systemBrightnessDarkTheme) { DB.instance.put( boxName: DB.boxNamePrefs, key: "systemBrightnessDarkTheme", - value: systemBrightnessDarkTheme); + value: systemBrightnessDarkTheme.name); _systemBrightnessDarkTheme = systemBrightnessDarkTheme; notifyListeners(); } } - Future _getSystemBrightnessDarkTheme() async { - return await DB.instance.get( + Future _getSystemBrightnessDarkTheme() async { + return ThemeTypeExt.fromName(await DB.instance.get( boxName: DB.boxNamePrefs, key: "systemBrightnessDarkTheme") as String? ?? - ThemeType.dark.name; + ThemeType.dark.name); } } diff --git a/lib/utilities/theme/color_theme.dart b/lib/utilities/theme/color_theme.dart index 9221cf2ca..fb7306de2 100644 --- a/lib/utilities/theme/color_theme.dart +++ b/lib/utilities/theme/color_theme.dart @@ -24,6 +24,29 @@ enum ThemeType { // adjust this file extension ThemeTypeExt on ThemeType { + static ThemeType fromName(String name) { + switch (name) { + case "light": + return ThemeType.light; + case "chan": + return ThemeType.chan; + case "dark": + return ThemeType.dark; + case "oceanBreeze": + return ThemeType.oceanBreeze; + case "oledBlack": + return ThemeType.oledBlack; + case "fruitSorbet": + return ThemeType.fruitSorbet; + case "forest": + return ThemeType.forest; + case "darkChans": + return ThemeType.darkChans; + default: + throw ArgumentError("Invalid theme name"); + } + } + StackColorTheme get colorTheme { switch (this) { case ThemeType.light: