Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
5 changes: 5 additions & 0 deletions packages/go_router/lib/src/route_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -189,6 +190,7 @@ abstract class ShellRouteData extends RouteData {
return ShellRoute(
builder: builder,
pageBuilder: pageBuilder,
parentNavigatorKey: parentNavigatorKey,
routes: routes,
navigatorKey: navigatorKey,
observers: observers,
Expand Down Expand Up @@ -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,
}) {
Expand Down Expand Up @@ -269,13 +272,15 @@ abstract class StatefulShellRouteData extends RouteData {
builder: builder,
pageBuilder: pageBuilder,
navigatorContainerBuilder: navigatorContainerBuilder,
parentNavigatorKey: parentNavigatorKey,
restorationScopeId: restorationScopeId,
);
}
return StatefulShellRoute.indexedStack(
branches: branches,
builder: builder,
pageBuilder: pageBuilder,
parentNavigatorKey: parentNavigatorKey,
restorationScopeId: restorationScopeId,
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
101 changes: 101 additions & 0 deletions packages/go_router/test/route_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down