mirror of
https://github.com/flutter/packages.git
synced 2025-06-30 14:47:22 +08:00
[go_router] Adds a parentNavigatorKey parameter to ShellRouteData. (#4409)
supports https://github.com/flutter/packages/pull/4356
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
## NEXT
|
||||
## 9.1.0
|
||||
|
||||
- Adds the parentNavigatorKey parameter to ShellRouteData and StatefulShellRouteData.
|
||||
- Fixes a typo in docs for `StatefulShellRoute.indexedStack(...)`.
|
||||
- Cleans some typos in the documentation and asserts.
|
||||
|
||||
|
@ -156,6 +156,7 @@ abstract class ShellRouteData extends RouteData {
|
||||
static ShellRoute $route<T extends ShellRouteData>({
|
||||
required T Function(GoRouterState) factory,
|
||||
GlobalKey<NavigatorState>? navigatorKey,
|
||||
GlobalKey<NavigatorState>? parentNavigatorKey,
|
||||
List<RouteBase> routes = const <RouteBase>[],
|
||||
List<NavigatorObserver>? observers,
|
||||
String? restorationScopeId,
|
||||
@ -189,6 +190,7 @@ abstract class ShellRouteData extends RouteData {
|
||||
return ShellRoute(
|
||||
builder: builder,
|
||||
pageBuilder: pageBuilder,
|
||||
parentNavigatorKey: parentNavigatorKey,
|
||||
routes: routes,
|
||||
navigatorKey: navigatorKey,
|
||||
observers: observers,
|
||||
@ -234,6 +236,7 @@ abstract class StatefulShellRouteData extends RouteData {
|
||||
static StatefulShellRoute $route<T extends StatefulShellRouteData>({
|
||||
required T Function(GoRouterState) factory,
|
||||
required List<StatefulShellBranch> branches,
|
||||
GlobalKey<NavigatorState>? parentNavigatorKey,
|
||||
ShellNavigationContainerBuilder? navigatorContainerBuilder,
|
||||
String? restorationScopeId,
|
||||
}) {
|
||||
@ -269,6 +272,7 @@ abstract class StatefulShellRouteData extends RouteData {
|
||||
builder: builder,
|
||||
pageBuilder: pageBuilder,
|
||||
navigatorContainerBuilder: navigatorContainerBuilder,
|
||||
parentNavigatorKey: parentNavigatorKey,
|
||||
restorationScopeId: restorationScopeId,
|
||||
);
|
||||
}
|
||||
@ -276,6 +280,7 @@ abstract class StatefulShellRouteData extends RouteData {
|
||||
branches: branches,
|
||||
builder: builder,
|
||||
pageBuilder: pageBuilder,
|
||||
parentNavigatorKey: parentNavigatorKey,
|
||||
restorationScopeId: restorationScopeId,
|
||||
);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
name: go_router
|
||||
description: A declarative router for Flutter based on Navigation 2 supporting
|
||||
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
|
||||
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
|
||||
|
||||
|
@ -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(
|
||||
path: '/build',
|
||||
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(
|
||||
'It should build the page from the overridden buildPage method',
|
||||
(WidgetTester tester) async {
|
||||
@ -257,6 +338,26 @@ void main() {
|
||||
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(
|
||||
|
Reference in New Issue
Block a user