Sub topic 8
Switching Tabs with `goBranch`
Assume you already know Dart, Flutter widgets, GoRouter, and the difference between a route and a navigator. This chapter focuses on selecting a branch in StatefulShellRoute, preserving each branch's history, and resetting the active branch when needed.
What `goBranch` does
A StatefulShellRoute gives each tab a separate navigator. The StatefulNavigationShell connects those navigators to your shell UI.
Its goBranch method selects one branch:
navigationShell.goBranch(index);The index matches the order of the StatefulShellBranch objects in the router. Index 0 selects the first branch, index 1 selects the second branch, and so on.
The shell also exposes currentIndex. Use it to keep the navigation control synchronized with the active branch.
flowchart TD
A[User taps a destination] --> B[onDestinationSelected receives index]
B --> C[navigationShell.goBranch index]
C --> D[StatefulShellRoute selects branch]
D --> E[NavigationShell displays that branch]Build a tab switcher
The following example creates two tabs. The NavigationBar reads its selected item from currentIndex and changes branches through goBranch.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const MyApp());
}
final router = GoRouter(
initialLocation: '/home',
routes: [
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return AppShell(navigationShell: navigationShell);
},
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const PageView(
title: 'Home',
color: Colors.blue,
),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const PageView(
title: 'Settings',
color: Colors.green,
),
),
],
),
],
),
],
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}
class AppShell extends StatelessWidget {
const AppShell({super.key, required this.navigationShell});
final StatefulNavigationShell navigationShell;
@override
Widget build(BuildContext context) {
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);
},
),
);
}
}
class PageView extends StatelessWidget {
const PageView({
super.key,
required this.title,
required this.color,
});
final String title;
final Color color;
@override
Widget build(BuildContext context) {
return ColoredBox(
color: color,
child: Center(
child: Text(title, style: const TextStyle(fontSize: 32)),
),
);
}
}StatefulShellRoute.indexedStack displays the selected branch and keeps the other branch navigators alive. The NavigationBar does not create routes itself. It only asks the shell to select a branch.
Reset the active tab
Often, tapping the already selected tab should return to that tab's root page. Pass initialLocation: true in that case:
onDestinationSelected: (index) {
final isCurrentBranch =
index == navigationShell.currentIndex;
navigationShell.goBranch(
index,
initialLocation: isCurrentBranch,
);
},When selecting a different tab, initialLocation is false, so go_router restores that branch's previous location. When selecting the current tab, it returns to the branch's initial route.
Common mistakes
- Use the same branch order for route definitions and
NavigationDestinationitems. - Set
selectedIndexfromcurrentIndex; do not maintain a second tab index in unrelated widget state. - Pass a valid zero-based branch index to
goBranch. - Do not use
context.goas a replacement forgoBranchwhen you want branch state restored. - If a tab unexpectedly resets, check whether
initialLocationis always set totrue.
Exercise
What should happen when the user is on a detail page in the Home branch, switches to Settings, and then switches back using goBranch(index) without initialLocation: true?
Show answer
The Home branch should return to its previous detail page because its navigator keeps that branch's location.