From cbef0f3da8e71888afb9607e708cec379573d51f Mon Sep 17 00:00:00 2001 From: Anishyou <123313052+Anishyou@users.noreply.github.com> Date: Thu, 25 Jan 2024 21:50:16 +0100 Subject: [PATCH] 188-BUG-Dark-Mode (#244) * bug solved * solved light mode text not showing --------- Co-authored-by: ge59dil --- lib/models/settings/setting_state_model.dart | 8 +++ lib/view_models/setting_view_model.dart | 20 ++++-- .../settings_view/settings_screen_view.dart | 66 ++++++++++++++++--- 3 files changed, 79 insertions(+), 15 deletions(-) diff --git a/lib/models/settings/setting_state_model.dart b/lib/models/settings/setting_state_model.dart index dc029724..729fb2a3 100644 --- a/lib/models/settings/setting_state_model.dart +++ b/lib/models/settings/setting_state_model.dart @@ -8,6 +8,8 @@ class SettingState { final List? userSettings; final AppError? error; final bool isDarkMode; + final bool isLightMode; + final bool isSystemDefault; final bool isPushNotificationsEnabled; final bool isDownloadWithWifiOnly; @@ -16,6 +18,8 @@ class SettingState { this.userSettings, this.error, this.isDarkMode = false, + this.isLightMode =false, + this.isSystemDefault =true, this.isPushNotificationsEnabled = true, this.isDownloadWithWifiOnly = true, }); @@ -25,6 +29,8 @@ class SettingState { List? userSettings, AppError? error, bool? isDarkMode, + bool? isLightMode, + bool? isSystemDefault, bool? isPushNotificationsEnabled, bool? isDownloadWithWifiOnly, }) { @@ -33,6 +39,8 @@ class SettingState { userSettings: userSettings ?? this.userSettings, error: error ?? this.error, isDarkMode: isDarkMode ?? this.isDarkMode, + isLightMode: isLightMode ?? this.isLightMode, + isSystemDefault: isSystemDefault ?? this.isSystemDefault, isPushNotificationsEnabled: isPushNotificationsEnabled ?? this.isPushNotificationsEnabled, isDownloadWithWifiOnly: diff --git a/lib/view_models/setting_view_model.dart b/lib/view_models/setting_view_model.dart index 81c47a66..8f25f670 100644 --- a/lib/view_models/setting_view_model.dart +++ b/lib/view_models/setting_view_model.dart @@ -50,18 +50,28 @@ class SettingViewModel extends StateNotifier { } Future loadThemePreference(SharedPreferences prefs) async { - final themePreference = prefs.getString('themeMode') ?? 'light'; - state = state.copyWith(isDarkMode: themePreference == 'dark'); + // Default to 'system' if no preference is set + final themePreference = prefs.getString('themeMode') ?? 'system'; + updateThemeMode(themePreference); } Future saveThemePreference(String theme, WidgetRef ref) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString('themeMode', theme); + updateThemeMode(theme); - state = state.copyWith(isDarkMode: theme == 'dark'); - + // Update the UI theme mode based on the selected preference ref.read(themeModeProvider.notifier).state = - theme == 'dark' ? ThemeMode.dark : ThemeMode.light; + theme == 'dark' ? ThemeMode.dark : theme == 'light' ? ThemeMode.light : ThemeMode.system; + } + + void updateThemeMode(String themePreference) { + // Update the state with the new theme preference + state = state.copyWith( + isDarkMode: themePreference == 'dark', + isLightMode: themePreference == 'light', + isSystemDefault: themePreference == 'system', + ); } Future loadNotificationPreference(SharedPreferences prefs) async { diff --git a/lib/views/settings_view/settings_screen_view.dart b/lib/views/settings_view/settings_screen_view.dart index 2c1c17fa..49df4d65 100644 --- a/lib/views/settings_view/settings_screen_view.dart +++ b/lib/views/settings_view/settings_screen_view.dart @@ -67,16 +67,7 @@ class _SettingsScreenState extends ConsumerState { }, ref: ref, ), - _buildSwitchListTile( - title: 'Dark mode', - value: settingState.isDarkMode, - onChanged: (value) { - ref - .read(settingViewModelProvider.notifier) - .saveThemePreference(value ? 'dark' : 'light', ref); - }, - ref: ref, - ), + _buildThemeSelectionTile(context, ref), _buildSwitchListTile( title: 'Download Over Wi-Fi only', value: settingState.isDownloadWithWifiOnly, @@ -120,6 +111,61 @@ class _SettingsScreenState extends ConsumerState { ); } + ListTile _buildThemeSelectionTile(BuildContext context, WidgetRef ref) { + // Get the current theme setting from the state + final settingState = ref.watch(settingViewModelProvider); + + String themeModeText; + if (settingState.isDarkMode) { + themeModeText = 'Dark Mode'; + } else if (settingState.isLightMode) { + themeModeText = 'Light Mode'; + } else { + themeModeText = 'System Default'; + } + + return ListTile( + title: const Text('Choose Theme'), + subtitle: Text(themeModeText), + trailing: const Icon(Icons.arrow_forward_ios), + onTap: () => _showThemeSelectionSheet(context, ref), + ); + } + + void _showThemeSelectionSheet(BuildContext context, WidgetRef ref) { + showModalBottomSheet( + context: context, + builder: (BuildContext context) { + return SafeArea( + child: Wrap( + children: [ + ListTile( + title: const Text('System Default'), + onTap: () { + ref.read(settingViewModelProvider.notifier).saveThemePreference('system', ref); + Navigator.pop(context); + }, + ), + ListTile( + title: const Text('Dark Mode'), + onTap: () { + ref.read(settingViewModelProvider.notifier).saveThemePreference('dark', ref); + Navigator.pop(context); + }, + ), + ListTile( + title: const Text('Light Mode'), + onTap: () { + ref.read(settingViewModelProvider.notifier).saveThemePreference('light', ref); + Navigator.pop(context); + }, + ), + ], + ), + ); + }, + ); + } ListTile _buildProfileTile(userState) { final settingState = ref.watch(settingViewModelProvider); final preferredNameSetting = settingState.userSettings?.firstWhere(