Sub topic 1
Quick refresher: GoRouter and GoRoute
Prerequisites
This chapter assumes you already know:
- Dart and Flutter basics:
StatelessWidget,Scaffold, andMaterialApp. - Basic
go_routeruse: aGoRouterwithGoRouteentries andcontext.go. - Path-based URLs such as
/home/details/42.
GoRouter and GoRoute
A GoRouter holds the app's route table. Each GoRoute maps a path to a builder that returns the screen for that path. Your app passes the router to MaterialApp.router.
Structuring the route tree with sub-routes
A GoRoute can contain its own routes. Child paths are written without a leading slash, and they are resolved relative to the parent. This forms a route tree.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
initialLocation: '/home',
routes: [
GoRoute(
path: '/home',
builder: (context, state) =>
const Scaffold(body: Center(child: Text('Home'))),
routes: [
GoRoute(
path: 'details/:id', // Full path: /home/details/:id
builder: (context, state) => Scaffold(
body: Center(
child: Text('Item ${state.pathParameters["id"]}'),
),
),
),
],
),
],
);Navigator keys and the root navigator
Each route is shown by a Navigator. The GoRouter has a root navigator, and you can give it a GlobalKey<NavigatorState> through navigatorKey. Keeping a reference to that key lets a route opt out of its parent navigator with parentNavigatorKey. Later sections use this to show a screen above a tab bar.
final GlobalKey<NavigatorState> rootNavigatorKey =
GlobalKey<NavigatorState>();
final GoRouter router = GoRouter(
navigatorKey: rootNavigatorKey,
routes: [/* ... */],
);> Note: keep the key in a top-level variable. Recreating it on each build breaks the navigator.
Why ShellRoute is not enough for tabs
A ShellRoute wraps its children in one shared inner navigator. Every child route shares one back stack. When a tab bar moves the user to another tab's path, the old tab's history is replaced and lost. A tab bar needs a separate stack per tab, so ShellRoute does not fit.
Introducing StatefulShellRoute.indexedStack
StatefulShellRoute.indexedStack gives each tab its own navigator. Each tab is a StatefulShellBranch with its own routes. The builder receives a navigationShell that you place in the layout, usually inside a Scaffold.
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
onDestinationSelected: (index) => navigationShell.goBranch(index),
destinations: const [
NavigationDestination(icon: Icon(Icons.home), label: 'Home'),
NavigationDestination(icon: Icon(Icons.settings), label: 'Settings'),
],
),
);
},
branches: [
StatefulShellBranch(
routes: [GoRoute(path: '/home', builder: (c, s) => const Placeholder())],
),
StatefulShellBranch(
routes: [GoRoute(path: '/settings', builder: (c, s) => const Placeholder())],
),
],
)Exercise
In a tab app, why does a plain ShellRoute lose a tab's back stack when the user switches tabs?
Show answer
A ShellRoute uses one inner navigator for all its children. Switching tabs replaces the current child route in that single stack, so the earlier tab's history is discarded. A StatefulShellRoute gives each branch its own navigator, which keeps each stack separate.