Skip to content

Commit

Permalink
Add AppRouteObserver and route name to all pageBuilders
Browse files Browse the repository at this point in the history
Still not working reliably. See: flutter/flutter#112185
  • Loading branch information
bizz84 committed Jan 11, 2025
1 parent 6ff7be6 commit 6e2d1cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/src/routing/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:starter_architecture_flutter_firebase/src/features/jobs/presenta
import 'package:starter_architecture_flutter_firebase/src/features/jobs/presentation/jobs_screen/jobs_screen.dart';
import 'package:starter_architecture_flutter_firebase/src/features/onboarding/data/onboarding_repository.dart';
import 'package:starter_architecture_flutter_firebase/src/features/onboarding/presentation/onboarding_screen.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/app_router_observer.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/go_router_refresh_stream.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/not_found_screen.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/scaffold_with_nested_navigation.dart';
Expand Down Expand Up @@ -47,6 +48,7 @@ GoRouter goRouter(Ref ref) {
initialLocation: '/signIn',
navigatorKey: _rootNavigatorKey,
debugLogDiagnostics: true,
observers: [AppRouterObserver()],
redirect: (context, state) {
final onboardingRepository =
ref.read(onboardingRepositoryProvider).requireValue;
Expand Down Expand Up @@ -80,21 +82,24 @@ GoRouter goRouter(Ref ref) {
GoRoute(
path: '/onboarding',
name: AppRoute.onboarding.name,
pageBuilder: (context, state) => const NoTransitionPage(
pageBuilder: (context, state) => NoTransitionPage(
name: AppRoute.onboarding.name,
child: OnboardingScreen(),
),
),
GoRoute(
path: '/signIn',
name: AppRoute.signIn.name,
pageBuilder: (context, state) => const NoTransitionPage(
pageBuilder: (context, state) => NoTransitionPage(
name: AppRoute.signIn.name,
child: CustomSignInScreen(),
),
),
// Stateful navigation based on:
// https://github.com/flutter/packages/blob/main/packages/go_router/example/lib/stateful_shell_route.dart
StatefulShellRoute.indexedStack(
pageBuilder: (context, state, navigationShell) => NoTransitionPage(
name: 'main',
child: ScaffoldWithNestedNavigation(navigationShell: navigationShell),
),
branches: [
Expand All @@ -104,7 +109,8 @@ GoRouter goRouter(Ref ref) {
GoRoute(
path: '/jobs',
name: AppRoute.jobs.name,
pageBuilder: (context, state) => const NoTransitionPage(
pageBuilder: (context, state) => NoTransitionPage(
name: AppRoute.jobs.name,
child: JobsScreen(),
),
routes: [
Expand All @@ -113,7 +119,8 @@ GoRouter goRouter(Ref ref) {
name: AppRoute.addJob.name,
parentNavigatorKey: _rootNavigatorKey,
pageBuilder: (context, state) {
return const MaterialPage(
return MaterialPage(
name: AppRoute.addJob.name,
fullscreenDialog: true,
child: EditJobScreen(),
);
Expand All @@ -125,6 +132,7 @@ GoRouter goRouter(Ref ref) {
pageBuilder: (context, state) {
final id = state.pathParameters['id']!;
return MaterialPage(
name: AppRoute.job.name,
child: JobEntriesScreen(jobId: id),
);
},
Expand All @@ -136,6 +144,7 @@ GoRouter goRouter(Ref ref) {
pageBuilder: (context, state) {
final jobId = state.pathParameters['id']!;
return MaterialPage(
name: AppRoute.addEntry.name,
fullscreenDialog: true,
child: EntryScreen(
jobId: jobId,
Expand All @@ -151,6 +160,7 @@ GoRouter goRouter(Ref ref) {
final entryId = state.pathParameters['eid']!;
final entry = state.extra as Entry?;
return MaterialPage(
name: AppRoute.entry.name,
child: EntryScreen(
jobId: jobId,
entryId: entryId,
Expand All @@ -166,6 +176,7 @@ GoRouter goRouter(Ref ref) {
final jobId = state.pathParameters['id'];
final job = state.extra as Job?;
return MaterialPage(
name: AppRoute.editJob.name,
fullscreenDialog: true,
child: EditJobScreen(jobId: jobId, job: job),
);
Expand All @@ -183,7 +194,8 @@ GoRouter goRouter(Ref ref) {
GoRoute(
path: '/entries',
name: AppRoute.entries.name,
pageBuilder: (context, state) => const NoTransitionPage(
pageBuilder: (context, state) => NoTransitionPage(
name: AppRoute.entries.name,
child: EntriesScreen(),
),
),
Expand All @@ -195,7 +207,8 @@ GoRouter goRouter(Ref ref) {
GoRoute(
path: '/account',
name: AppRoute.profile.name,
pageBuilder: (context, state) => const NoTransitionPage(
pageBuilder: (context, state) => NoTransitionPage(
name: AppRoute.profile.name,
child: CustomProfileScreen(),
),
),
Expand Down
32 changes: 32 additions & 0 deletions lib/src/routing/app_router_observer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:developer';

import 'package:flutter/material.dart';

class AppRouterObserver extends NavigatorObserver {
static const _name = 'Navigation';

@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
_logNavigation(route.settings.name, 'push');
}

@override
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
_logNavigation(route.settings.name, 'pop');
}

@override
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
if (newRoute != null) {
_logNavigation(newRoute.settings.name, 'replace');
}
}

void _logNavigation(String? routeName, String action) {
if (routeName != null) {
log('Route: $routeName, action: $action', name: _name);
} else {
log('Route name is missing', name: _name);
}
}
}

0 comments on commit 6e2d1cb

Please sign in to comment.