Sub topic 11
Opening Full-Screen Routes Above the Tab Bar
This chapter assumes that you know Dart and Flutter widgets, understand nested routes and navigator stacks, and have basic experience with GoRouter, GoRoute, and StatefulShellRoute.
A StatefulShellRoute usually keeps a navigation bar visible while each tab displays its own navigator. That behavior is useful for pages such as home details or settings screens. Some pages should cover the entire shell instead. Examples include photo viewers, checkout screens, media players, and login pages.
Two navigator levels
A stateful shell normally contains several branch navigators. Each branch owns the routes for one tab. The shell itself is displayed by a root navigator.
A route without a special navigator key is placed in the nearest branch navigator. It appears inside that branch, so the tab bar remains visible. A route with parentNavigatorKey: rootNavigatorKey is placed in the root navigator. It appears above the shell and covers the tab bar.
The key must be the same key supplied to GoRouter. The route does not become full-screen merely because its widget uses a larger layout. Its navigator determines which other widgets it covers.
Configuring a route above the shell
The following router has two tabs. The details/:id route stays inside the home branch. The photo route uses the root navigator, so it covers the complete shell.
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']!,
);
},
),
GoRoute(
path: 'photo',
parentNavigatorKey: rootNavigatorKey,
builder: (context, state) => const PhotoScreen(),
),
],
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
],
),
],
),
],
);The full-screen route still has the location /home/photo because it is nested under the home route. Its parentNavigatorKey changes where the page is displayed, not how the URL is structured.
From HomeScreen, navigate to it with:
context.push('/home/photo');Calling context.pop() from PhotoScreen removes the root-level page and reveals the shell again. The selected tab and its branch stack remain available.
Choosing the correct placement
Use the branch navigator when the destination belongs to the tab:
- A product detail page opened from a shopping tab.
- A message thread opened from an inbox tab.
- A settings subsection opened from the settings tab.
Use the root navigator when the destination should temporarily take over the application:
- A photo or video viewer.
- A checkout flow.
- A modal-like authentication page.
- A document editor that should hide tab navigation.
A common mistake is defining a second GlobalKey<NavigatorState> for the photo route. That key represents a different navigator and does not identify the root navigator. Reuse the single root key instead.
Exercise
The home details page should keep the tab bar visible, but the photo viewer should hide it. Which route property belongs on the photo route?
Show answer
Set parentNavigatorKey: rootNavigatorKey on the photo route. Leave the details route without that property so it uses the home branch navigator.