Skip to content

Commit 84b086f

Browse files
authored
[go_router] Adds a parentNavigatorKey parameter to ShellRouteData. (flutter#4409)
supports flutter#4356
1 parent 88aa497 commit 84b086f

4 files changed

Lines changed: 109 additions & 2 deletions

File tree

packages/go_router/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 9.1.0
22

3+
- Adds the parentNavigatorKey parameter to ShellRouteData and StatefulShellRouteData.
34
- Fixes a typo in docs for `StatefulShellRoute.indexedStack(...)`.
45
- Cleans some typos in the documentation and asserts.
56

packages/go_router/lib/src/route_data.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ abstract class ShellRouteData extends RouteData {
156156
static ShellRoute $route<T extends ShellRouteData>({
157157
required T Function(GoRouterState) factory,
158158
GlobalKey<NavigatorState>? navigatorKey,
159+
GlobalKey<NavigatorState>? parentNavigatorKey,
159160
List<RouteBase> routes = const <RouteBase>[],
160161
List<NavigatorObserver>? observers,
161162
String? restorationScopeId,
@@ -189,6 +190,7 @@ abstract class ShellRouteData extends RouteData {
189190
return ShellRoute(
190191
builder: builder,
191192
pageBuilder: pageBuilder,
193+
parentNavigatorKey: parentNavigatorKey,
192194
routes: routes,
193195
navigatorKey: navigatorKey,
194196
observers: observers,
@@ -234,6 +236,7 @@ abstract class StatefulShellRouteData extends RouteData {
234236
static StatefulShellRoute $route<T extends StatefulShellRouteData>({
235237
required T Function(GoRouterState) factory,
236238
required List<StatefulShellBranch> branches,
239+
GlobalKey<NavigatorState>? parentNavigatorKey,
237240
ShellNavigationContainerBuilder? navigatorContainerBuilder,
238241
String? restorationScopeId,
239242
}) {
@@ -269,13 +272,15 @@ abstract class StatefulShellRouteData extends RouteData {
269272
builder: builder,
270273
pageBuilder: pageBuilder,
271274
navigatorContainerBuilder: navigatorContainerBuilder,
275+
parentNavigatorKey: parentNavigatorKey,
272276
restorationScopeId: restorationScopeId,
273277
);
274278
}
275279
return StatefulShellRoute.indexedStack(
276280
branches: branches,
277281
builder: builder,
278282
pageBuilder: pageBuilder,
283+
parentNavigatorKey: parentNavigatorKey,
279284
restorationScopeId: restorationScopeId,
280285
);
281286
}

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 9.0.3
4+
version: 9.1.0
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/route_data_test.dart

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ class _ShellRouteDataBuilder extends ShellRouteData {
3030
);
3131
}
3232

33+
class _ShellRouteDataWithKey extends ShellRouteData {
34+
const _ShellRouteDataWithKey(this.key);
35+
36+
final Key key;
37+
38+
@override
39+
Widget builder(
40+
BuildContext context,
41+
GoRouterState state,
42+
Widget navigator,
43+
) =>
44+
SizedBox(
45+
key: key,
46+
child: navigator,
47+
);
48+
}
49+
50+
class _GoRouteDataBuildWithKey extends GoRouteData {
51+
const _GoRouteDataBuildWithKey(this.key);
52+
final Key key;
53+
@override
54+
Widget build(BuildContext context, GoRouterState state) => SizedBox(key: key);
55+
}
56+
3357
final GoRoute _goRouteDataBuild = GoRouteData.$route(
3458
path: '/build',
3559
factory: (GoRouterState state) => const _GoRouteDataBuild(),
@@ -211,6 +235,63 @@ void main() {
211235
},
212236
);
213237

238+
testWidgets(
239+
'It should build the page from the overridden build method',
240+
(WidgetTester tester) async {
241+
final GlobalKey<NavigatorState> root = GlobalKey<NavigatorState>();
242+
final GlobalKey<NavigatorState> inner = GlobalKey<NavigatorState>();
243+
final GoRouter goRouter = GoRouter(
244+
navigatorKey: root,
245+
initialLocation: '/child/test',
246+
routes: <RouteBase>[
247+
ShellRouteData.$route(
248+
factory: (GoRouterState state) =>
249+
const _ShellRouteDataWithKey(Key('under-shell')),
250+
routes: <RouteBase>[
251+
GoRouteData.$route(
252+
path: '/child',
253+
factory: (GoRouterState state) =>
254+
const _GoRouteDataBuildWithKey(Key('under')),
255+
routes: <RouteBase>[
256+
ShellRouteData.$route(
257+
factory: (GoRouterState state) =>
258+
const _ShellRouteDataWithKey(Key('above-shell')),
259+
navigatorKey: inner,
260+
parentNavigatorKey: root,
261+
routes: <RouteBase>[
262+
GoRouteData.$route(
263+
parentNavigatorKey: inner,
264+
path: 'test',
265+
factory: (GoRouterState state) =>
266+
const _GoRouteDataBuildWithKey(Key('above')),
267+
),
268+
],
269+
),
270+
]),
271+
],
272+
),
273+
],
274+
);
275+
await tester.pumpWidget(MaterialApp.router(
276+
routerConfig: goRouter,
277+
));
278+
expect(find.byKey(const Key('under-shell')), findsNothing);
279+
expect(find.byKey(const Key('under')), findsNothing);
280+
281+
expect(find.byKey(const Key('above-shell')), findsOneWidget);
282+
expect(find.byKey(const Key('above')), findsOneWidget);
283+
284+
goRouter.pop();
285+
await tester.pumpAndSettle();
286+
287+
expect(find.byKey(const Key('under-shell')), findsOneWidget);
288+
expect(find.byKey(const Key('under')), findsOneWidget);
289+
290+
expect(find.byKey(const Key('above-shell')), findsNothing);
291+
expect(find.byKey(const Key('above')), findsNothing);
292+
},
293+
);
294+
214295
testWidgets(
215296
'It should build the page from the overridden buildPage method',
216297
(WidgetTester tester) async {
@@ -257,6 +338,26 @@ void main() {
257338
expect(find.byKey(const Key('page-builder')), findsOneWidget);
258339
},
259340
);
341+
342+
test('Can assign parent navigator key', () {
343+
final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
344+
final StatefulShellRoute route = StatefulShellRouteData.$route(
345+
parentNavigatorKey: key,
346+
factory: (GoRouterState state) =>
347+
const _StatefulShellRouteDataPageBuilder(),
348+
branches: <StatefulShellBranch>[
349+
StatefulShellBranchData.$branch(
350+
routes: <RouteBase>[
351+
GoRouteData.$route(
352+
path: '/child',
353+
factory: (GoRouterState state) => const _GoRouteDataBuild(),
354+
),
355+
],
356+
),
357+
],
358+
);
359+
expect(route.parentNavigatorKey, key);
360+
});
260361
});
261362

262363
testWidgets(

0 commit comments

Comments
 (0)