diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index af3840e6cf35..c84f25c81808 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +# 17.1.0 + +- Adds `TypedGoRouteParameter` annotation to override parameter names in `TypedGoRoute` constructors. + ## 17.0.1 - Fixes an issue where `onEnter` blocking causes navigation stack loss (stale state restoration). diff --git a/packages/go_router/lib/src/route_data.dart b/packages/go_router/lib/src/route_data.dart index dfbd9cced43b..557e8755a0c7 100644 --- a/packages/go_router/lib/src/route_data.dart +++ b/packages/go_router/lib/src/route_data.dart @@ -619,3 +619,29 @@ class NoOpPage extends Page { Route createRoute(BuildContext context) => throw UnsupportedError('Should never be called'); } + +/// Annotation to override the URI name for a route parameter. +@Target({TargetKind.parameter}) +class TypedGoRouteParameter { + /// Annotation to override the URI name for a route parameter. + const TypedGoRouteParameter({this.name}); + + /// The name of the parameter in the URI. + /// + /// If `null`, the kebab-case version of the parameter name will be used. + /// + /// For example: + /// ```dart + /// class MyRoute extends GoRouteData with $MyRoute { + /// const MyRoute({ + /// @TypedGoRouteParameter(name: 'custom_name') this.myParameter, + /// }); + /// final String myParameter; + /// } + /// ``` + /// + /// This will result in a route that matches + /// `/my-route?custom_name=some_value` instead of the default + /// `/my-route?my-parameter=some_value`. + final String? name; +} diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 12a911bf9faa..55ad56b1058c 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: 17.0.1 +version: 17.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 diff --git a/packages/go_router/test/route_data_test.dart b/packages/go_router/test/route_data_test.dart index 044a449ce86b..24b417b0c4aa 100644 --- a/packages/go_router/test/route_data_test.dart +++ b/packages/go_router/test/route_data_test.dart @@ -794,4 +794,10 @@ void main() { ), ); }); + + test('TypedGoRouteParameter stores the name', () { + const parameter = TypedGoRouteParameter(name: 'customName'); + + expect(parameter.name, 'customName'); + }); }