Sub topic 3
Shared layouts with ShellRoute
This chapter assumes you know GoRoute, context.go, MaterialApp.router, and ChangeNotifier. It covers the rest of the plan briefly.
Shared layouts with ShellRoute
A ShellRoute wraps several child routes in one shared widget, such as a scaffold with an app bar. The child changes as the location changes, but the shell stays on screen.
final GoRouter router = GoRouter(
initialLocation: '/home',
routes: <RouteBase>[
ShellRoute(
builder: (context, state, child) => Scaffold(
appBar: AppBar(title: const Text('Shop')),
body: child,
),
routes: <RouteBase>[
GoRoute(path: '/home', builder: (c, s) => const Text('Home')),
GoRoute(path: '/cart', builder: (c, s) => const Text('Cart')),
],
),
],
);Bottom navigation that keeps state with StatefulShellRoute
StatefulShellRoute.indexedStack gives each tab its own navigator. Each tab keeps its own back stack and widget state when the user switches away. Call navigationShell.goBranch(index) from the navigation bar to switch tabs.
Custom pages and transitions
Use pageBuilder instead of builder to return a CustomTransitionPage. The example below replaces the default platform transition with a fade.
GoRoute(
path: '/details',
pageBuilder: (context, state) => CustomTransitionPage<void>(
key: state.pageKey,
child: const Text('Details'),
transitionsBuilder: (context, animation, secondary, child) =>
FadeTransition(opacity: animation, child: child),
),
),Handling unknown locations
Set errorBuilder on the router to show a page when no route matches the location.
final GoRouter router = GoRouter(
routes: <RouteBase>[/* your routes */],
errorBuilder: (context, state) =>
Scaffold(body: Center(child: Text('Not found: ${state.uri}'))),
);> callout note: ...
Deep links and web URLs
On the web, the browser address bar maps directly to a route path. On mobile, an incoming deep link passes its URI to the router the same way. A matching path opens its route, and redirects still run. A non-matching path shows errorBuilder.
Type-safe routes with go_router_builder
Annotate route classes with GoRouteData and @TypedGoRoute. The build_runner tool then generates the route table and typed navigation methods, so the compiler checks path parameters.
@TypedGoRoute<BookRoute>(path: '/books/:id')
class BookRoute extends GoRouteData {
const BookRoute({required this.id});
final String id;
@override
Widget build(BuildContext context, GoRouterState state) => Text('Book $id');
}
// Navigate with: BookRoute(id: '42').go(context);Testing routes and redirects
Pump the router inside MaterialApp.router, navigate, and check the result. The test below checks that a signed-out user lands on the login page.
testWidgets('signed-out user sees login', (tester) async {
await tester.pumpWidget(MaterialApp.router(routerConfig: router));
await tester.pumpAndSettle();
expect(find.text('Login'), findsOneWidget);
});Exercise
A user taps a tab in a StatefulShellRoute, then returns to the first tab. Does the first tab keep its scroll position, and why?
Show answer
Yes. Each branch of an indexedStack shell has its own navigator that stays alive, so its state persists while another branch is shown.