Sub topic 5
Introducing StatefulShellRoute.indexedStack
This chapter assumes that you know Dart and Flutter widgets, understand Navigator and route stacks, and have used GoRouter with GoRoute and context.go. The goal is to understand why StatefulShellRoute.indexedStack is useful before adding nested detail pages or advanced guards.
Why a shell route needs state
A shell route places shared UI around a page. A common example is a Scaffold with a bottom navigation bar. The shell stays visible while the content changes.
ShellRoute can provide this layout, but it does not automatically give each tab an independent navigation history. If the user opens a detail page in one tab and then changes tabs, the detail page can be replaced when the first tab is rebuilt. This makes tabs behave like simple links instead of separate sections of an app.
StatefulShellRoute solves this problem. It creates a separate Navigator for each branch. A branch is one independent route subtree, such as Home or Settings. Each branch can keep its own current location and route stack.
The indexedStack constructor uses an indexed stack to display the active branch while keeping the other branch navigators mounted.
The basic structure
The following router defines two tab branches. The shell builder receives a StatefulNavigationShell. This widget represents the active branch area and provides methods for changing branches.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final router = GoRouter(
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(),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
],
),
],
),
],
);
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return const Center(child: Text('Home'));
}
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) {
return const Center(child: Text('Settings'));
}
}How the pieces work together
The StatefulShellRoute owns the shared shell layout. Its branches define the independent navigators. The StatefulNavigationShell connects those navigators to the UI.
When the user selects a destination, goBranch changes the active branch. The indexed stack keeps the inactive branch mounted, so its navigation stack and widget state remain available.
This is different from calling context.go with a new location and rebuilding one shared page area. With StatefulShellRoute.indexedStack, the route location changes, but each branch retains its own navigation context.
Exercise
Exercise
What does StatefulShellRoute.indexedStack preserve that a basic shared shell may not preserve?
Show answer
It preserves a separate Navigator, route stack, and mounted widget state for each branch.