Sub topic 6
Defining tabs as StatefulShellBranch objects
This chapter assumes that you know Dart and Flutter widgets, understand Navigator and route stacks, and have used GoRouter, GoRoute, and context.go. The goal is to define each tab as an independent route subtree.
What a StatefulShellBranch represents
A StatefulShellBranch is one branch of a StatefulShellRoute. In a tabbed app, each branch usually represents one tab, such as Home or Settings.
A branch contains:
- One or more
GoRouteobjects. - A route subtree for that tab.
- Its own navigator.
- Its own navigation stack and widget state.
The first route in a branch is usually the tab's root route. Child routes then represent pages opened from that tab.
A branch is different from a simple item in a NavigationBar. The navigation bar only changes which branch is visible. The branch owns the routes and history behind that tab.
Defining the branches
The following router defines two branches. The Home branch has a nested details route. The Settings branch has a nested account route.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final rootNavigatorKey = GlobalKey<NavigatorState>();
final router = GoRouter(
navigatorKey: rootNavigatorKey,
initialLocation: '/home',
routes: [
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
destinations: const [
NavigationDestination(
icon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
onDestinationSelected: (index) {
navigationShell.goBranch(index);
},
),
);
},
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const HomeScreen(),
routes: [
GoRoute(
path: 'details/:id',
builder: (context, state) {
return DetailsScreen(
id: state.pathParameters['id']!,
);
},
),
],
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
routes: [
GoRoute(
path: 'account',
builder: (context, state) => const AccountScreen(),
),
],
),
],
),
],
),
],
);
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () => context.go('/home/details/42'),
child: const Text('Open details'),
),
);
}
}
class DetailsScreen extends StatelessWidget {
const DetailsScreen({required this.id, super.key});
final String id;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Details $id')),
body: Center(child: Text('Item $id')),
);
}
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () => context.go('/settings/account'),
child: const Text('Open account'),
),
);
}
}
class AccountScreen extends StatelessWidget {
const AccountScreen({super.key});
@override
Widget build(BuildContext context) {
return const Center(child: Text('Account'));
}
}The root paths /home and /settings identify the tabs. The child paths use relative syntax: details/:id becomes /home/details/:id, and account becomes /settings/account.
Choosing branch configuration
You can give a branch its own navigator key when another part of the app needs to address that navigator directly.
final homeNavigatorKey = GlobalKey<NavigatorState>();
StatefulShellBranch(
navigatorKey: homeNavigatorKey,
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const HomeScreen(),
),
],
)Use distinct keys for distinct navigators. Do not reuse the root navigator key as a branch key. The root navigator belongs to the application-level route hierarchy, while a branch navigator manages pages inside one tab.
| Configuration | Purpose |
|---|---|
| `routes` | Defines the branch's route subtree |
| `navigatorKey` | Identifies the branch navigator |
| `initialLocation` | Sets the branch's starting location when configured |
| `restorationScopeId` | Supports restoration of branch navigation state |
Branch design rules
Treat a branch as a small application with its own entry page and child pages.
- Put pages that belong to one tab under that tab's root route.
- Use relative child paths to make the ownership clear.
- Keep shared destinations outside the branch when they should not belong to one tab.
- Add a branch navigator key only when you need explicit navigator control.
- Let the shell widget handle tab selection; let each branch handle its own pages.
Exercise
In the example, what URL is produced by the child route details/:id under the Home branch when id is 42?
Show answer
The URL is /home/details/42. The child path is relative to the parent route /home.