Sub topic 11
Opening full-screen routes above the tab bar
This chapter assumes you can build the tabbed shell from the earlier chapters. That means a StatefulShellRoute.indexedStack, two or more StatefulShellBranch objects, and a shell widget that wraps a StatefulNavigationShell. It also assumes you know what a GlobalKey<NavigatorState> is and why GoRouter accepts one.
The problem: pages stay inside the tab
Each branch of a StatefulShellRoute has its own Navigator. When you push /home/details/7, go_router places that page in the Home branch's navigator. The page appears inside the shell's body, so the bottom NavigationBar stays visible. This is correct for most detail pages.
Some screens should cover everything, including the tab bar:
- a full-screen photo or video viewer
- a checkout or onboarding flow
- a login screen shown from inside the app
- a camera or document scanner
These pages must live in a navigator that sits above the shell. That navigator is the root navigator.
How the navigator hierarchy works
GoRouter creates one root Navigator. The shell widget is a page in that root navigator, and each branch has its own nested navigator inside the shell's body.
The parentNavigatorKey property of GoRoute chooses which navigator receives the route's page. If you leave it unset, a nested route goes into the nearest enclosing navigator, which is the branch navigator. If you set it to the root navigator's key, the page goes into the root navigator instead.
Step 1: give GoRouter a root key
The key you pass as GoRouter(navigatorKey: ...) becomes the root navigator's key. Create it once, at top level, and reuse that same object everywhere.
final GlobalKey<NavigatorState> rootNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'root');If you omit navigatorKey on GoRouter, go_router creates its own key internally. Your own key then does not match any navigator, and a route that references it fails.
Step 2: set parentNavigatorKey on the route
A route can stay nested under its tab and still open above the shell. The URL keeps the tab's prefix, and only the page's location in the widget tree changes.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GlobalKey<NavigatorState> rootNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'root');
final GoRouter router = GoRouter(
navigatorKey: rootNavigatorKey,
initialLocation: '/home',
routes: [
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return ScaffoldWithNavBar(navigationShell: navigationShell);
},
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const HomeScreen(),
routes: [
GoRoute(
path: 'details/:id',
builder: (context, state) =>
DetailsScreen(id: state.pathParameters['id']!),
),
GoRoute(
path: 'photo/:id',
parentNavigatorKey: rootNavigatorKey,
builder: (context, state) =>
PhotoViewerScreen(id: state.pathParameters['id']!),
),
],
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
],
),
],
),
],
);
void main() => runApp(MaterialApp.router(routerConfig: router));
class ScaffoldWithNavBar extends StatelessWidget {
const ScaffoldWithNavBar({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,
initialLocation: index == navigationShell.currentIndex,
),
),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Column(
children: [
ListTile(
title: const Text('Details 7 (inside the tab)'),
onTap: () => context.push('/home/details/7'),
),
ListTile(
title: const Text('Photo 7 (full screen)'),
onTap: () => context.push('/home/photo/7'),
),
],
),
);
}
}
class DetailsScreen extends StatelessWidget {
const DetailsScreen({super.key, required this.id});
final String id;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text('Details $id')),
body: const Center(child: Text('The tab bar is still visible.')),
);
}
class PhotoViewerScreen extends StatelessWidget {
const PhotoViewerScreen({super.key, required this.id});
final String id;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text('Photo $id')),
body: const Center(child: Text('No tab bar here.')),
);
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: const Center(child: Text('Settings')),
);
}Tapping "Details 7" opens a page inside the Home tab. Tapping "Photo 7" opens a page above the shell. The back arrow on the photo page pops the root navigator and returns you to the Home tab exactly as you left it.
What happens to the tab underneath
The shell stays mounted below the full-screen page. The Home branch still holds its stack, its widgets, and their state, such as scroll positions. When you pop the photo page, nothing needs to be rebuilt from scratch.
This only holds when you open the page with push, or with go to a location that includes the tab's own route. In the example above, the location /home/photo/7 matches /home and then photo/:id. The router builds the Home page in the branch navigator and the photo page in the root navigator.
Nested route or top-level route
You have two ways to get a full-screen page. They differ in how the URL is structured and how the shell is treated.
| Approach | URL example | Shell underneath | Typical use |
|---|---|---|---|
| Nested route with `parentNavigatorKey: rootNavigatorKey` | `/home/photo/7` | Stays mounted, showing the parent tab | Content that belongs to one tab, such as a photo viewer |
| Top-level `GoRoute` outside the shell | `/login` | Not part of the match when you use `go` | Flows that belong to no tab, such as login or onboarding |
Top-level routes already use the root navigator, so they need no parentNavigatorKey. If you call context.go('/login'), the new location replaces the whole match list, and the shell is no longer in it. Call context.push('/login') if you want the shell to stay underneath.
Keeping the right back behavior
The back button and context.pop() act on the topmost navigator with something to pop. With a full-screen page open, that is the root navigator, and one pop returns you to the tab. Inside a tab, popping a detail page returns to the tab's first page. When the branch is back at its first page and the system back button is pressed, go_router follows the root navigator's rules, so on Android the app can leave.
Do not call Navigator.of(context).pop() on a page whose context is in a different navigator than you expect. Prefer context.pop() from go_router. It pops from the router's current match list, which keeps the URL and the stack in sync.
Dialogs and bottom sheets
showDialog and showModalBottomSheet use the root navigator by default, because their useRootNavigator argument defaults to true. They already cover the tab bar. If you pass useRootNavigator: false, they appear inside the current tab. That is a deliberate choice for some designs, for example a sheet that must leave the tab bar visible.
When you need a navigator context outside any widget, for example in a notification handler, use the key:
final BuildContext? rootContext = rootNavigatorKey.currentContext;
if (rootContext != null) {
showDialog<void>(
context: rootContext,
builder: (_) => const AlertDialog(title: Text('Signed out')),
);
}currentContext is null until the first frame has built the navigator, so check it before use.
Pitfalls
- Key mismatch. The
parentNavigatorKeymust be the same object you passed toGoRouter(navigatorKey: ...). A secondGlobalKeycreated elsewhere does not point at any navigator. - Key created inside
build. AGlobalKeycreated on each rebuild is a new object each time. Keep keys in top-level or long-lived fields. - Key used twice. One
GlobalKeycan be attached to only one navigator. Do not reuse the root key as thenavigatorKeyof aStatefulShellBranch. - Expecting a tab switch. A route with
parentNavigatorKeyset to the root does not changenavigationShell.currentIndexby itself. Navigating to/home/photo/7from the Settings tab does switch to Home first, because the match list includes the/homeroute, but you should test this on your own route tree. - A top-level route that shares a path prefix. If both a top-level route and a nested route resolve to the same full path, the first match in the list wins. Keep full paths unique.
- Sheets that hide the bar unexpectedly. If a bottom sheet covers the tab bar and you did not want that, pass
useRootNavigator: false.
Exercise
Exercise
In the example, /home/details/7 shows the tab bar, but /home/photo/7 does not. Both routes sit under /home in the same branch. What single property causes the difference, what value must it have, and what happens to the Home tab's stack while the photo page is open?
Show answer
The photo/:id route sets parentNavigatorKey: rootNavigatorKey, where rootNavigatorKey is the same GlobalKey<NavigatorState> passed to GoRouter(navigatorKey: ...). That places the photo page in the root navigator, above the shell. The details route has no parentNavigatorKey, so it goes into the Home branch's navigator, below the tab bar. While the photo page is open, the shell and the Home branch stay mounted underneath, so the Home stack and its widget state are intact when you pop back.
Exercise
You add a /login route as a sibling of the StatefulShellRoute in the top-level routes list. Do you need parentNavigatorKey on it? What is the difference between context.go('/login') and context.push('/login') for the tabs?
Show answer
No. Top-level routes already build into the root navigator. context.go('/login') replaces the match list, so the shell is no longer in it. context.push('/login') adds the login page on top of the current stack, so the shell and its tabs stay mounted underneath.