From 040ff6a80d17bdfc59508dfb56b121f689eef524 Mon Sep 17 00:00:00 2001 From: Haroon Amjad <> Date: Fri, 6 Mar 2026 15:31:10 +0300 Subject: [PATCH] Dark mode persistence added in the app --- lib/core/dependencies.dart | 2 +- .../profile_settings_view_model.dart | 18 ++++++++++++++++++ lib/main.dart | 3 +++ .../home/data/landing_page_data.dart | 4 ++-- lib/presentation/home/landing_page.dart | 6 +++--- .../home/widgets/welcome_widget.dart | 2 +- .../smartwatches/widgets/health_chart.dart | 1 + lib/widgets/datepicker_widget.dart | 1 + lib/widgets/phone_number_input.dart | 2 +- 9 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/core/dependencies.dart b/lib/core/dependencies.dart index e7ac0515..4266aa3b 100644 --- a/lib/core/dependencies.dart +++ b/lib/core/dependencies.dart @@ -224,7 +224,7 @@ class AppDependencies { authenticationRepo: getIt(), cacheService: getIt(), navigationService: getIt(), dialogService: getIt(), appState: getIt(), errorHandlerService: getIt(), localAuthService: getIt()), ); - getIt.registerLazySingleton(() => ProfileSettingsViewModel()); + getIt.registerLazySingleton(() => ProfileSettingsViewModel(cacheService: getIt())); getIt.registerLazySingleton(() => DateRangeSelectorRangeViewModel()); diff --git a/lib/features/profile_settings/profile_settings_view_model.dart b/lib/features/profile_settings/profile_settings_view_model.dart index 2f788c69..e001d1bd 100644 --- a/lib/features/profile_settings/profile_settings_view_model.dart +++ b/lib/features/profile_settings/profile_settings_view_model.dart @@ -1,14 +1,32 @@ import 'package:flutter/foundation.dart'; +import 'package:hmg_patient_app_new/services/cache_service.dart'; import 'package:hmg_patient_app_new/theme/colors.dart'; class ProfileSettingsViewModel extends ChangeNotifier { + static const String _darkModeKey = 'is_dark_mode'; + + final CacheService _cacheService; + bool _isDarkMode = false; bool get isDarkMode => _isDarkMode; + ProfileSettingsViewModel({required CacheService cacheService}) + : _cacheService = cacheService; + + /// Call once at app startup (before the first frame) to restore the + /// persisted dark-mode preference. + void loadDarkMode() { + final saved = _cacheService.getBool(key: _darkModeKey); + _isDarkMode = saved ?? false; + AppColors.isDarkMode = _isDarkMode; + // No notifyListeners() here — we are called before build. + } + void toggleDarkMode(bool value) { _isDarkMode = value; AppColors.isDarkMode = value; + _cacheService.saveBool(key: _darkModeKey, value: value); notifyListeners(); } diff --git a/lib/main.dart b/lib/main.dart index 023404a6..d38c9aab 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -111,6 +111,9 @@ Future callInitializations() async { SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]); HttpOverrides.global = MyHttpOverrides(); await callAppStateInitializations(); + + // Restore persisted dark-mode preference before the first frame. + getIt.get().loadDarkMode(); } void main() async { diff --git a/lib/presentation/home/data/landing_page_data.dart b/lib/presentation/home/data/landing_page_data.dart index 1fefb8ce..b74e57f3 100644 --- a/lib/presentation/home/data/landing_page_data.dart +++ b/lib/presentation/home/data/landing_page_data.dart @@ -13,8 +13,8 @@ class LandingPageData { title: LocaleKeys.emergency, subtitle: LocaleKeys.services2, backgroundColor: AppColors.primaryRedColor, - iconColor: AppColors.whiteColor, - textColor: AppColors.whiteColor, + iconColor: Colors.white, + textColor: Colors.white, isBold: true, ), ServiceCardData( diff --git a/lib/presentation/home/landing_page.dart b/lib/presentation/home/landing_page.dart index e8482bf0..607a47f7 100644 --- a/lib/presentation/home/landing_page.dart +++ b/lib/presentation/home/landing_page.dart @@ -296,9 +296,9 @@ class _LandingPageState extends State { context.navigateWithName(AppRoutes.userInfoSelection); }, padding: EdgeInsetsGeometry.zero, - backgroundColor: Color(0xFF2B353E), - borderColor: Color(0xFF2B353E), - textColor: AppColors.whiteColor, + backgroundColor: AppColors.primaryRedColor, + borderColor: AppColors.primaryRedColor, + textColor: Colors.white, fontSize: 14.f, fontWeight: FontWeight.w600, borderRadius: 12.r, diff --git a/lib/presentation/home/widgets/welcome_widget.dart b/lib/presentation/home/widgets/welcome_widget.dart index 1bb17b5b..4ccc126a 100644 --- a/lib/presentation/home/widgets/welcome_widget.dart +++ b/lib/presentation/home/widgets/welcome_widget.dart @@ -40,7 +40,7 @@ class WelcomeWidget extends StatelessWidget { mainAxisSize: MainAxisSize.min, children: [ Flexible(child: name.toText16(weight: FontWeight.w500, textOverflow: TextOverflow.ellipsis, maxlines: 1, height: 1)), - const Icon(Icons.keyboard_arrow_down, size: 20, color: Colors.black), + Icon(Icons.keyboard_arrow_down, size: 20, color: AppColors.greyTextColor), ], ), ], diff --git a/lib/presentation/smartwatches/widgets/health_chart.dart b/lib/presentation/smartwatches/widgets/health_chart.dart index 37474133..e3c368ba 100644 --- a/lib/presentation/smartwatches/widgets/health_chart.dart +++ b/lib/presentation/smartwatches/widgets/health_chart.dart @@ -1,5 +1,6 @@ import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/material.dart'; +import 'package:hmg_patient_app_new/theme/colors.dart'; class HealthChart extends StatelessWidget { final List spots; diff --git a/lib/widgets/datepicker_widget.dart b/lib/widgets/datepicker_widget.dart index 1a5444a0..e2891da4 100644 --- a/lib/widgets/datepicker_widget.dart +++ b/lib/widgets/datepicker_widget.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:hmg_patient_app_new/extensions/widget_extensions.dart'; +import 'package:hmg_patient_app_new/theme/colors.dart'; class DatePickerWidget extends StatelessWidget { final String labelText; diff --git a/lib/widgets/phone_number_input.dart b/lib/widgets/phone_number_input.dart index 83ddcf16..793c8062 100644 --- a/lib/widgets/phone_number_input.dart +++ b/lib/widgets/phone_number_input.dart @@ -67,7 +67,7 @@ class _PhoneNumberInputState extends State { ) .toList(), onChanged: _onCountryCodeChanged, - icon: const Icon( + icon: Icon( Icons.keyboard_arrow_down_rounded, color: AppColors.mainPurple, ),