diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 82df3001eeb..d33dd28b237 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 14.1.2 + +- Fixes issue that path parameters are not set when using the `goBranch`. + ## 14.1.1 - Fixes correctness of the state provided in the `onExit`. diff --git a/packages/go_router/lib/src/route.dart b/packages/go_router/lib/src/route.dart index b93520b95aa..8b23e54f31a 100644 --- a/packages/go_router/lib/src/route.dart +++ b/packages/go_router/lib/src/route.dart @@ -1134,7 +1134,12 @@ class StatefulNavigationShell extends StatefulWidget { /// Recursively traverses the routes of the provided StackedShellBranch to /// find the first GoRoute, from which a full path will be derived. final GoRoute route = branch.defaultRoute!; - return _router.configuration.locationForRoute(route)!; + final List parameters = []; + patternToRegExp(route.path, parameters); + assert(parameters.isEmpty); + final String fullPath = _router.configuration.locationForRoute(route)!; + return patternToPath( + fullPath, shellRouteContext.routerState.pathParameters); } } diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 5aac8fbec38..0694585b544 100644 --- a/packages/go_router/pubspec.yaml +++ b/packages/go_router/pubspec.yaml @@ -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: 14.1.1 +version: 14.1.2 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 diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index 639db182f51..f395faf906f 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -3956,6 +3956,50 @@ void main() { expect(statefulWidgetKey.currentState?.counter, equals(0)); }); + testWidgets( + 'Navigates to correct nested navigation tree in StatefulShellRoute ' + 'and maintains path parameters', (WidgetTester tester) async { + StatefulNavigationShell? routeState; + + final List routes = [ + GoRoute( + path: '/:id', + builder: (_, __) => const Placeholder(), + routes: [ + StatefulShellRoute.indexedStack( + builder: (BuildContext context, GoRouterState state, + StatefulNavigationShell navigationShell) { + routeState = navigationShell; + return navigationShell; + }, + branches: [ + StatefulShellBranch(routes: [ + GoRoute( + path: 'a', + builder: (BuildContext context, GoRouterState state) => + Text('a id is ${state.pathParameters['id']}'), + ), + ]), + StatefulShellBranch(routes: [ + GoRoute( + path: 'b', + builder: (BuildContext context, GoRouterState state) => + Text('b id is ${state.pathParameters['id']}'), + ), + ]), + ], + ), + ]) + ]; + + await createRouter(routes, tester, initialLocation: '/123/a'); + expect(find.text('a id is 123'), findsOneWidget); + + routeState!.goBranch(1); + await tester.pumpAndSettle(); + expect(find.text('b id is 123'), findsOneWidget); + }); + testWidgets('Maintains state for nested StatefulShellRoute', (WidgetTester tester) async { final GlobalKey rootNavigatorKey =