Skip to content

Commit 1d6710b

Browse files
committed
refactor(router): replace AdNavigatorObserver with GoRouterObserver
- Remove AdNavigatorObserver import and usage - Add GlobalKey<NavigatorState> navigatorKey parameter - Implement custom GoRouterObserver using navigatorKey - Update router creation to use GoRouterObserver - Remove unnecessary comments and clean up code
1 parent 4f917b8 commit 1d6710b

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

lib/router/router.dart

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import 'package:flutter_news_app_mobile_client_full_source_code/account/view/man
1313
import 'package:flutter_news_app_mobile_client_full_source_code/account/view/manage_followed_items/topics/add_topic_to_follow_page.dart';
1414
import 'package:flutter_news_app_mobile_client_full_source_code/account/view/manage_followed_items/topics/followed_topics_list_page.dart';
1515
import 'package:flutter_news_app_mobile_client_full_source_code/account/view/saved_headlines_page.dart';
16-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_navigator_observer.dart';
1716
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
1817
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
1918
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
@@ -66,13 +65,12 @@ GoRouter createRouter({
6665
required DataRepository<Source> sourcesRepository,
6766
required DataRepository<UserAppSettings> userAppSettingsRepository,
6867
required DataRepository<UserContentPreferences>
69-
userContentPreferencesRepository,
68+
userContentPreferencesRepository,
7069
required DataRepository<RemoteConfig> remoteConfigRepository,
7170
required DataRepository<User> userRepository,
7271
required local_config.AppEnvironment environment,
7372
required AdService adService,
74-
required AdNavigatorObserver
75-
adNavigatorObserver, // Accept AdNavigatorObserver
73+
required GlobalKey<NavigatorState> navigatorKey, // Add navigatorKey
7674
}) {
7775
// Instantiate AccountBloc once to be shared
7876
final accountBloc = AccountBloc(
@@ -95,7 +93,7 @@ GoRouter createRouter({
9593
initialLocation: '/',
9694
debugLogDiagnostics: true,
9795
observers: [
98-
adNavigatorObserver, // Pass the AdNavigatorObserver to GoRouter
96+
GoRouterObserver(navigatorKey: navigatorKey), // Use GoRouterObserver with navigatorKey
9997
],
10098
// --- Redirect Logic ---
10199
redirect: (BuildContext context, GoRouterState state) {
@@ -402,7 +400,6 @@ GoRouter createRouter({
402400
);
403401
},
404402
),
405-
// Removed separate AccountBloc creation here
406403
],
407404
child: AppShell(navigationShell: navigationShell),
408405
);
@@ -449,7 +446,7 @@ GoRouter createRouter({
449446
);
450447
},
451448
),
452-
// Sub-route for notifications (placeholder) - MOVED HERE
449+
// Sub-route for notifications (placeholder)
453450
GoRoute(
454451
path: Routes.notifications,
455452
name: Routes.notificationsName,
@@ -500,9 +497,8 @@ GoRouter createRouter({
500497
create: (context) => SourcesFilterBloc(
501498
sourcesRepository: context
502499
.read<DataRepository<Source>>(),
503-
countriesRepository: // Added missing repository
504-
context
505-
.read<DataRepository<Country>>(),
500+
countriesRepository:
501+
context.read<DataRepository<Country>>(),
506502
userContentPreferencesRepository: context
507503
.read<DataRepository<UserContentPreferences>>(),
508504
appBloc: context.read<AppBloc>(),
@@ -528,7 +524,6 @@ GoRouter createRouter({
528524
final initialSelection =
529525
state.extra as List<Country>?;
530526
return MaterialPage(
531-
// fullscreenDialog: true,
532527
child: BlocProvider(
533528
create: (context) => CountriesFilterBloc(
534529
countriesRepository: context
@@ -786,3 +781,29 @@ GoRouter createRouter({
786781
],
787782
);
788783
}
784+
785+
/// A custom [NavigatorObserver] that provides access to the [NavigatorState]
786+
/// via a [GlobalKey].
787+
///
788+
/// This is used to obtain a [BuildContext] for services that need to interact
789+
/// with the widget tree (e.g., showing dialogs) but are not directly part
790+
/// of the tree themselves.
791+
class GoRouterObserver extends NavigatorObserver {
792+
/// Creates a [GoRouterObserver].
793+
GoRouterObserver({required this.navigatorKey});
794+
795+
/// The [GlobalKey] used to access the [NavigatorState].
796+
final GlobalKey<NavigatorState> navigatorKey;
797+
798+
@override
799+
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
800+
super.didPush(route, previousRoute);
801+
// You can add logging or other logic here if needed.
802+
}
803+
804+
@override
805+
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
806+
super.didPop(route, previousRoute);
807+
// You can add logging or other logic here if needed.
808+
}
809+
}

0 commit comments

Comments
 (0)