[go_router] Adds a parentNavigatorKey parameter to ShellRouteData. (#4409)

supports https://github.com/flutter/packages/pull/4356
This commit is contained in:
chunhtai
2023-07-19 09:00:58 -07:00
committed by GitHub
parent 88aa497bfe
commit 84b086f258
4 changed files with 109 additions and 2 deletions

View File

@ -1,5 +1,6 @@
## NEXT ## 9.1.0
- Adds the parentNavigatorKey parameter to ShellRouteData and StatefulShellRouteData.
- Fixes a typo in docs for `StatefulShellRoute.indexedStack(...)`. - Fixes a typo in docs for `StatefulShellRoute.indexedStack(...)`.
- Cleans some typos in the documentation and asserts. - Cleans some typos in the documentation and asserts.

View File

@ -156,6 +156,7 @@ abstract class ShellRouteData extends RouteData {
static ShellRoute $route<T extends ShellRouteData>({ static ShellRoute $route<T extends ShellRouteData>({
required T Function(GoRouterState) factory, required T Function(GoRouterState) factory,
GlobalKey<NavigatorState>? navigatorKey, GlobalKey<NavigatorState>? navigatorKey,
GlobalKey<NavigatorState>? parentNavigatorKey,
List<RouteBase> routes = const <RouteBase>[], List<RouteBase> routes = const <RouteBase>[],
List<NavigatorObserver>? observers, List<NavigatorObserver>? observers,
String? restorationScopeId, String? restorationScopeId,
@ -189,6 +190,7 @@ abstract class ShellRouteData extends RouteData {
return ShellRoute( return ShellRoute(
builder: builder, builder: builder,
pageBuilder: pageBuilder, pageBuilder: pageBuilder,
parentNavigatorKey: parentNavigatorKey,
routes: routes, routes: routes,
navigatorKey: navigatorKey, navigatorKey: navigatorKey,
observers: observers, observers: observers,
@ -234,6 +236,7 @@ abstract class StatefulShellRouteData extends RouteData {
static StatefulShellRoute $route<T extends StatefulShellRouteData>({ static StatefulShellRoute $route<T extends StatefulShellRouteData>({
required T Function(GoRouterState) factory, required T Function(GoRouterState) factory,
required List<StatefulShellBranch> branches, required List<StatefulShellBranch> branches,
GlobalKey<NavigatorState>? parentNavigatorKey,
ShellNavigationContainerBuilder? navigatorContainerBuilder, ShellNavigationContainerBuilder? navigatorContainerBuilder,
String? restorationScopeId, String? restorationScopeId,
}) { }) {
@ -269,6 +272,7 @@ abstract class StatefulShellRouteData extends RouteData {
builder: builder, builder: builder,
pageBuilder: pageBuilder, pageBuilder: pageBuilder,
navigatorContainerBuilder: navigatorContainerBuilder, navigatorContainerBuilder: navigatorContainerBuilder,
parentNavigatorKey: parentNavigatorKey,
restorationScopeId: restorationScopeId, restorationScopeId: restorationScopeId,
); );
} }
@ -276,6 +280,7 @@ abstract class StatefulShellRouteData extends RouteData {
branches: branches, branches: branches,
builder: builder, builder: builder,
pageBuilder: pageBuilder, pageBuilder: pageBuilder,
parentNavigatorKey: parentNavigatorKey,
restorationScopeId: restorationScopeId, restorationScopeId: restorationScopeId,
); );
} }

View File

@ -1,7 +1,7 @@
name: go_router name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more deep linking, data-driven routes and more
version: 9.0.3 version: 9.1.0
repository: https://github.com/flutter/packages/tree/main/packages/go_router repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22 issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

View File

@ -30,6 +30,30 @@ class _ShellRouteDataBuilder extends ShellRouteData {
); );
} }
class _ShellRouteDataWithKey extends ShellRouteData {
const _ShellRouteDataWithKey(this.key);
final Key key;
@override
Widget builder(
BuildContext context,
GoRouterState state,
Widget navigator,
) =>
SizedBox(
key: key,
child: navigator,
);
}
class _GoRouteDataBuildWithKey extends GoRouteData {
const _GoRouteDataBuildWithKey(this.key);
final Key key;
@override
Widget build(BuildContext context, GoRouterState state) => SizedBox(key: key);
}
final GoRoute _goRouteDataBuild = GoRouteData.$route( final GoRoute _goRouteDataBuild = GoRouteData.$route(
path: '/build', path: '/build',
factory: (GoRouterState state) => const _GoRouteDataBuild(), factory: (GoRouterState state) => const _GoRouteDataBuild(),
@ -211,6 +235,63 @@ void main() {
}, },
); );
testWidgets(
'It should build the page from the overridden build method',
(WidgetTester tester) async {
final GlobalKey<NavigatorState> root = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> inner = GlobalKey<NavigatorState>();
final GoRouter goRouter = GoRouter(
navigatorKey: root,
initialLocation: '/child/test',
routes: <RouteBase>[
ShellRouteData.$route(
factory: (GoRouterState state) =>
const _ShellRouteDataWithKey(Key('under-shell')),
routes: <RouteBase>[
GoRouteData.$route(
path: '/child',
factory: (GoRouterState state) =>
const _GoRouteDataBuildWithKey(Key('under')),
routes: <RouteBase>[
ShellRouteData.$route(
factory: (GoRouterState state) =>
const _ShellRouteDataWithKey(Key('above-shell')),
navigatorKey: inner,
parentNavigatorKey: root,
routes: <RouteBase>[
GoRouteData.$route(
parentNavigatorKey: inner,
path: 'test',
factory: (GoRouterState state) =>
const _GoRouteDataBuildWithKey(Key('above')),
),
],
),
]),
],
),
],
);
await tester.pumpWidget(MaterialApp.router(
routerConfig: goRouter,
));
expect(find.byKey(const Key('under-shell')), findsNothing);
expect(find.byKey(const Key('under')), findsNothing);
expect(find.byKey(const Key('above-shell')), findsOneWidget);
expect(find.byKey(const Key('above')), findsOneWidget);
goRouter.pop();
await tester.pumpAndSettle();
expect(find.byKey(const Key('under-shell')), findsOneWidget);
expect(find.byKey(const Key('under')), findsOneWidget);
expect(find.byKey(const Key('above-shell')), findsNothing);
expect(find.byKey(const Key('above')), findsNothing);
},
);
testWidgets( testWidgets(
'It should build the page from the overridden buildPage method', 'It should build the page from the overridden buildPage method',
(WidgetTester tester) async { (WidgetTester tester) async {
@ -257,6 +338,26 @@ void main() {
expect(find.byKey(const Key('page-builder')), findsOneWidget); expect(find.byKey(const Key('page-builder')), findsOneWidget);
}, },
); );
test('Can assign parent navigator key', () {
final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
final StatefulShellRoute route = StatefulShellRouteData.$route(
parentNavigatorKey: key,
factory: (GoRouterState state) =>
const _StatefulShellRouteDataPageBuilder(),
branches: <StatefulShellBranch>[
StatefulShellBranchData.$branch(
routes: <RouteBase>[
GoRouteData.$route(
path: '/child',
factory: (GoRouterState state) => const _GoRouteDataBuild(),
),
],
),
],
);
expect(route.parentNavigatorKey, key);
});
}); });
testWidgets( testWidgets(