diff --git a/lib/main.dart b/lib/main.dart index 9f222380c..dc4b6877e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,6 +8,7 @@ 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'; @@ -56,6 +57,7 @@ import 'package:stackwallet/utilities/db_version_migration.dart'; import 'package:stackwallet/utilities/enums/backup_frequency_type.dart'; 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'; @@ -161,6 +163,8 @@ void main() async { (await StackFileSystem.applicationHiveDirectory()).path); await Hive.openBox(DB.boxNameDBInfo); + await Hive.openBox(DB.boxNamePrefs); + await Prefs.instance.init(); // Desktop migrate handled elsewhere (currently desktop_login_view.dart) if (!Util.isDesktop) { @@ -335,8 +339,25 @@ class _MaterialAppWithThemeState extends ConsumerState @override void initState() { - final colorScheme = DB.instance - .get(boxName: DB.boxNameTheme, key: "colorScheme") as String?; + final String? colorScheme; + if (ref.read(prefsChangeNotifierProvider).enableSystemBrightness) { + final brightness = + SchedulerBinding.instance.platformDispatcher.platformBrightness; + switch (brightness) { + case Brightness.dark: + colorScheme = + ref.read(prefsChangeNotifierProvider).systemBrightnessDarkTheme; + break; + case Brightness.light: + colorScheme = + ref.read(prefsChangeNotifierProvider).systemBrightnessLightTheme; + break; + } + } else { + colorScheme = + DB.instance.get(boxName: DB.boxNameTheme, key: "colorScheme") + as String?; + } StackColorTheme colorTheme; switch (colorScheme) { 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 04642b98f..9f50402b0 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 @@ -102,42 +102,105 @@ class AppearanceSettingsView extends ConsumerWidget { height: 10, ), 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, + 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( - "Choose Theme", + "System brightness", style: STextStyles.titleBold12(context), textAlign: TextAlign.left, ), - const Padding( - padding: EdgeInsets.all(10), - child: ThemeOptionsView(), - ), + SizedBox( + height: 20, + width: 40, + child: DraggableSwitchButton( + isOn: ref.watch( + prefsChangeNotifierProvider.select( + (value) => value + .enableSystemBrightness), + ), + onValueChanged: (newValue) { + ref + .read( + prefsChangeNotifierProvider) + .enableSystemBrightness = newValue; + }, + ), + ) ], ), - ], + ), + ); + }, + ), + ), + if (!ref.watch( + prefsChangeNotifierProvider + .select((value) => value.enableSystemBrightness), + )) + const SizedBox( + height: 10, + ), + if (!ref.watch( + prefsChangeNotifierProvider + .select((value) => value.enableSystemBrightness), + )) + 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: [ + Text( + "Choose Theme", + style: + STextStyles.titleBold12(context), + textAlign: TextAlign.left, + ), + const Padding( + padding: EdgeInsets.all(10), + child: ThemeOptionsView(), + ), + ], + ), + ], + ), ), ), ), - ), ], ), ), 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 4c53cc5c5..4452d0467 100644 --- a/lib/pages_desktop_specific/settings/settings_menu/appearance_settings.dart +++ b/lib/pages_desktop_specific/settings/settings_menu/appearance_settings.dart @@ -118,6 +118,44 @@ class _AppearanceOptionSettings thickness: 0.5, ), ), + Padding( + padding: const EdgeInsets.all(10.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + "System brightness", + style: STextStyles.desktopTextExtraSmall(context) + .copyWith( + color: Theme.of(context) + .extension()! + .textDark), + 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 Padding( + padding: EdgeInsets.all(10.0), + child: Divider( + thickness: 0.5, + ), + ), Padding( padding: const EdgeInsets.all(10.0), child: Row( diff --git a/lib/utilities/prefs.dart b/lib/utilities/prefs.dart index 5fcbfc64a..78551a695 100644 --- a/lib/utilities/prefs.dart +++ b/lib/utilities/prefs.dart @@ -4,6 +4,7 @@ import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/enums/backup_frequency_type.dart'; import 'package:stackwallet/utilities/enums/languages_enum.dart'; import 'package:stackwallet/utilities/enums/sync_type_enum.dart'; +import 'package:stackwallet/utilities/theme/color_theme.dart'; import 'package:uuid/uuid.dart'; class Prefs extends ChangeNotifier { @@ -41,6 +42,9 @@ class Prefs extends ChangeNotifier { _userId = await _getUserId(); _signupEpoch = await _getSignupEpoch(); _enableCoinControl = await _getEnableCoinControl(); + _enableSystemBrightness = await _getEnableSystemBrightness(); + _systemBrightnessLightTheme = await _getSystemBrightnessLightTheme(); + _systemBrightnessDarkTheme = await _getSystemBrightnessDarkTheme(); _initialized = true; } @@ -669,4 +673,75 @@ class Prefs extends ChangeNotifier { boxName: DB.boxNamePrefs, key: "enableCoinControl") as bool? ?? false; } + + // follow system brightness + + bool _enableSystemBrightness = false; + + bool get enableSystemBrightness => _enableSystemBrightness; + + set enableSystemBrightness(bool enableSystemBrightness) { + if (_enableSystemBrightness != enableSystemBrightness) { + DB.instance.put( + boxName: DB.boxNamePrefs, + key: "enableSystemBrightness", + value: enableSystemBrightness); + _enableSystemBrightness = enableSystemBrightness; + notifyListeners(); + } + } + + Future _getEnableSystemBrightness() async { + return await DB.instance.get( + boxName: DB.boxNamePrefs, key: "enableSystemBrightness") as bool? ?? + false; + } + + // system brightness light theme name + + String _systemBrightnessLightTheme = ThemeType.light.name; + + String get systemBrightnessLightTheme => _systemBrightnessLightTheme; + + set systemBrightnessLightTheme(String systemBrightnessLightTheme) { + if (this.systemBrightnessLightTheme != systemBrightnessLightTheme) { + DB.instance.put( + boxName: DB.boxNamePrefs, + key: "systemBrightnessLightTheme", + value: systemBrightnessLightTheme); + _systemBrightnessLightTheme = systemBrightnessLightTheme; + notifyListeners(); + } + } + + Future _getSystemBrightnessLightTheme() async { + return await DB.instance.get( + boxName: DB.boxNamePrefs, + key: "systemBrightnessLightTheme") as String? ?? + ThemeType.light.name; + } + + // system brightness dark theme name + + String _systemBrightnessDarkTheme = ThemeType.dark.name; + + String get systemBrightnessDarkTheme => _systemBrightnessDarkTheme; + + set systemBrightnessDarkTheme(String systemBrightnessDarkTheme) { + if (this.systemBrightnessDarkTheme != systemBrightnessDarkTheme) { + DB.instance.put( + boxName: DB.boxNamePrefs, + key: "systemBrightnessDarkTheme", + value: systemBrightnessDarkTheme); + _systemBrightnessDarkTheme = systemBrightnessDarkTheme; + notifyListeners(); + } + } + + Future _getSystemBrightnessDarkTheme() async { + return await DB.instance.get( + boxName: DB.boxNamePrefs, + key: "systemBrightnessDarkTheme") as String? ?? + ThemeType.dark.name; + } }