@@ -11,6 +11,13 @@ final GlobalKey<NavigatorState> _rootNavigatorKey =
11
11
GlobalKey <NavigatorState >(debugLabel: 'root' );
12
12
final GlobalKey <NavigatorState > _tabANavigatorKey =
13
13
GlobalKey <NavigatorState >(debugLabel: 'tabANav' );
14
+ final GlobalKey <NavigatorState > _tabBNavigatorKey =
15
+ GlobalKey <NavigatorState >(debugLabel: 'tabBNav' );
16
+ final GlobalKey <NavigatorState > _tabB1NavigatorKey =
17
+ GlobalKey <NavigatorState >(debugLabel: 'tabB1Nav' );
18
+ final GlobalKey <NavigatorState > _tabB2NavigatorKey =
19
+ GlobalKey <NavigatorState >(debugLabel: 'tabB2Nav' );
20
+
14
21
@visibleForTesting
15
22
// ignore: public_member_api_docs
16
23
final GlobalKey <TabbedRootScreenState > tabbedRootScreenKey =
@@ -87,11 +94,15 @@ class NestedTabNavigationExampleApp extends StatelessWidget {
87
94
88
95
// The route branch for the second tab of the bottom navigation bar.
89
96
StatefulShellBranch (
97
+ navigatorKey: _tabBNavigatorKey,
98
+ // To enable preloading of the initial locations of branches, pass
99
+ // `true` for the parameter `preload` (`false` is default).
100
+ preload: true ,
90
101
// StatefulShellBranch will automatically use the first descendant
91
102
// GoRoute as the initial location of the branch. If another route
92
103
// is desired, specify the location of it using the defaultLocation
93
104
// parameter.
94
- // defaultLocation: '/b2 ',
105
+ // defaultLocation: '/b1 ',
95
106
routes: < RouteBase > [
96
107
StatefulShellRoute (
97
108
builder: (BuildContext context, GoRouterState state,
@@ -119,44 +130,53 @@ class NestedTabNavigationExampleApp extends StatelessWidget {
119
130
// This bottom tab uses a nested shell, wrapping sub routes in a
120
131
// top TabBar.
121
132
branches: < StatefulShellBranch > [
122
- StatefulShellBranch (routes: < GoRoute > [
123
- GoRoute (
124
- path: '/b1' ,
125
- builder: (BuildContext context, GoRouterState state) =>
126
- const TabScreen (
127
- label: 'B1' , detailsPath: '/b1/details' ),
128
- routes: < RouteBase > [
133
+ StatefulShellBranch (
134
+ navigatorKey: _tabB1NavigatorKey,
135
+ routes: < GoRoute > [
129
136
GoRoute (
130
- path: 'details ' ,
137
+ path: '/b1 ' ,
131
138
builder:
132
139
(BuildContext context, GoRouterState state) =>
133
- const DetailsScreen (
134
- label: 'B1' ,
135
- withScaffold: false ,
136
- ),
140
+ const TabScreen (
141
+ label: 'B1' , detailsPath: '/b1/details' ),
142
+ routes: < RouteBase > [
143
+ GoRoute (
144
+ path: 'details' ,
145
+ builder:
146
+ (BuildContext context, GoRouterState state) =>
147
+ const DetailsScreen (
148
+ label: 'B1' ,
149
+ withScaffold: false ,
150
+ ),
151
+ ),
152
+ ],
137
153
),
138
- ],
139
- ),
140
- ]),
141
- StatefulShellBranch (routes: < GoRoute > [
142
- GoRoute (
143
- path: '/b2' ,
144
- builder: (BuildContext context, GoRouterState state) =>
145
- const TabScreen (
146
- label: 'B2' , detailsPath: '/b2/details' ),
147
- routes: < RouteBase > [
154
+ ]),
155
+ StatefulShellBranch (
156
+ navigatorKey: _tabB2NavigatorKey,
157
+ // To enable preloading for all nested branches, set
158
+ // `preload` to `true` (`false` is default).
159
+ preload: true ,
160
+ routes: < GoRoute > [
148
161
GoRoute (
149
- path: 'details ' ,
162
+ path: '/b2 ' ,
150
163
builder:
151
164
(BuildContext context, GoRouterState state) =>
152
- const DetailsScreen (
153
- label: 'B2' ,
154
- withScaffold: false ,
155
- ),
165
+ const TabScreen (
166
+ label: 'B2' , detailsPath: '/b2/details' ),
167
+ routes: < RouteBase > [
168
+ GoRoute (
169
+ path: 'details' ,
170
+ builder:
171
+ (BuildContext context, GoRouterState state) =>
172
+ const DetailsScreen (
173
+ label: 'B2' ,
174
+ withScaffold: false ,
175
+ ),
176
+ ),
177
+ ],
156
178
),
157
- ],
158
- ),
159
- ]),
179
+ ]),
160
180
],
161
181
),
162
182
],
@@ -619,6 +639,11 @@ class TabScreen extends StatelessWidget {
619
639
620
640
@override
621
641
Widget build (BuildContext context) {
642
+ /// If preloading is enabled on the top StatefulShellRoute, this will be
643
+ /// printed directly after the app has been started, but only for the route
644
+ /// that is the initial location ('/b1')
645
+ debugPrint ('Building TabScreen - $label ' );
646
+
622
647
return Center (
623
648
child: Column (
624
649
mainAxisSize: MainAxisSize .min,
0 commit comments