diff --git a/lib/main.dart b/lib/main.dart index aa266871..87746f14 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -370,6 +370,7 @@ class MyApp extends StatelessWidget { darkTheme: AppTheme.getDarkTheme(isArabic), themeMode: profileVm.isDarkMode ? ThemeMode.dark : ThemeMode.light, navigatorKey: getIt.get().navigatorKey, + navigatorObservers: [getIt.get().routeObserver], ); }, ); diff --git a/lib/presentation/authentication/saved_login_screen.dart b/lib/presentation/authentication/saved_login_screen.dart index 19080ae8..bea93d36 100644 --- a/lib/presentation/authentication/saved_login_screen.dart +++ b/lib/presentation/authentication/saved_login_screen.dart @@ -16,6 +16,7 @@ import 'package:hmg_patient_app_new/features/authentication/authentication_view_ import 'package:hmg_patient_app_new/generated/locale_keys.g.dart'; import 'package:hmg_patient_app_new/presentation/authentication/login.dart'; import 'package:hmg_patient_app_new/presentation/home/navigation_screen.dart'; +import 'package:hmg_patient_app_new/services/navigation_service.dart'; import 'package:hmg_patient_app_new/theme/colors.dart'; import 'package:hmg_patient_app_new/widgets/appbar/app_bar_widget.dart'; import 'package:hmg_patient_app_new/widgets/bottomsheet/generic_bottom_sheet.dart'; @@ -30,11 +31,24 @@ class SavedLogin extends StatefulWidget { _SavedLogin createState() => _SavedLogin(); } -class _SavedLogin extends State { +class _SavedLogin extends State with RouteAware { LoginTypeEnum loginType = LoginTypeEnum.sms; late AuthenticationViewModel authVm; late AppState appState; bool? isOther; + PageRoute? _pageRoute; + + void _assignSavedLoginData() { + authVm.phoneNumberController.text = appState.getSelectDeviceByImeiRespModelElement!.mobile!.startsWith("0") + ? appState.getSelectDeviceByImeiRespModelElement!.mobile!.replaceFirst("0", "") + : appState.getSelectDeviceByImeiRespModelElement!.mobile!; + authVm.nationalIdController.text = appState.getSelectDeviceByImeiRespModelElement!.identificationNo!; + + if (isOther == true) { + authVm.phoneNumberController.text = appState.getSelectDeviceByImeiRespModelElement!.mobile!; + authVm.selectedCountrySignup = CountryEnum.others; + } + } @override void initState() { @@ -43,19 +57,10 @@ class _SavedLogin extends State { appState = getIt.get(); loginType = LoginTypeExtension.fromValue(appState.getSelectDeviceByImeiRespModelElement!.logInType!)!; - authVm.phoneNumberController.text = appState.getSelectDeviceByImeiRespModelElement!.mobile!.startsWith("0") - ? appState.getSelectDeviceByImeiRespModelElement!.mobile!.replaceFirst("0", "") - : appState.getSelectDeviceByImeiRespModelElement!.mobile!; - authVm.nationalIdController.text = appState.getSelectDeviceByImeiRespModelElement!.identificationNo!; - // Check if this is an "Others" country user isOther = appState.getSelectDeviceByImeiRespModelElement!.isOther == true; - // Set country to Others if isOther flag is true - this ensures _Others APIs are called - if (isOther!) { - authVm.phoneNumberController.text = appState.getSelectDeviceByImeiRespModelElement!.mobile!; - authVm.selectedCountrySignup = CountryEnum.others; - } + _assignSavedLoginData(); if (loginType == LoginTypeEnum.fingerprint || loginType == LoginTypeEnum.face) { authVm.loginWithFingerPrintFace(() {}); @@ -63,6 +68,30 @@ class _SavedLogin extends State { super.initState(); } + @override + void didChangeDependencies() { + super.didChangeDependencies(); + final route = ModalRoute.of(context); + if (route is PageRoute && route != _pageRoute) { + if (_pageRoute != null) { + getIt.get().routeObserver.unsubscribe(this); + } + _pageRoute = route; + getIt.get().routeObserver.subscribe(this, route); + } + } + + @override + void didPopNext() { + _assignSavedLoginData(); + } + + @override + void dispose() { + getIt.get().routeObserver.unsubscribe(this); + super.dispose(); + } + @override Widget build(BuildContext context) { return Scaffold( diff --git a/lib/services/navigation_service.dart b/lib/services/navigation_service.dart index a83d4e3d..018e46b6 100644 --- a/lib/services/navigation_service.dart +++ b/lib/services/navigation_service.dart @@ -6,6 +6,7 @@ import 'package:hmg_patient_app_new/routes/app_routes.dart'; class NavigationService { final GlobalKey navigatorKey = GlobalKey(); + final RouteObserver> routeObserver = RouteObserver>(); BuildContext? get context => navigatorKey.currentContext;