diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md index 304b579e8ab22..a1c74c7a63946 100644 --- a/packages/go_router_builder/CHANGELOG.md +++ b/packages/go_router_builder/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.0.1 + +* Supports name parameter for `TypedGoRoute`. ## 2.0.0 * Updates the documentation to go_router v7.0.0. diff --git a/packages/go_router_builder/example/lib/simple_example.dart b/packages/go_router_builder/example/lib/simple_example.dart index 6253fc6e9d166..dee29d0c867c4 100644 --- a/packages/go_router_builder/example/lib/simple_example.dart +++ b/packages/go_router_builder/example/lib/simple_example.dart @@ -28,6 +28,7 @@ class App extends StatelessWidget { @TypedGoRoute( path: '/', + name: 'Home', routes: >[ TypedGoRoute(path: 'family/:familyId') ], diff --git a/packages/go_router_builder/example/lib/simple_example.g.dart b/packages/go_router_builder/example/lib/simple_example.g.dart index 410c914bacb73..d3e624d63dea9 100644 --- a/packages/go_router_builder/example/lib/simple_example.g.dart +++ b/packages/go_router_builder/example/lib/simple_example.g.dart @@ -14,6 +14,7 @@ List get $appRoutes => [ RouteBase get $homeRoute => GoRouteData.$route( path: '/', + name: 'Home', factory: $HomeRouteExtension._fromState, routes: [ GoRouteData.$route( diff --git a/packages/go_router_builder/lib/src/route_config.dart b/packages/go_router_builder/lib/src/route_config.dart index 50d7e7738a953..a67f33bab0591 100644 --- a/packages/go_router_builder/lib/src/route_config.dart +++ b/packages/go_router_builder/lib/src/route_config.dart @@ -37,6 +37,7 @@ class InfoIterable extends IterableBase { class RouteConfig { RouteConfig._( this._path, + this._name, this._routeDataClass, this._parent, this._key, @@ -75,6 +76,7 @@ class RouteConfig { final bool isShellRoute = type.element.name == 'TypedShellRoute'; String? path; + String? name; if (!isShellRoute) { final ConstantReader pathValue = reader.read('path'); @@ -85,6 +87,9 @@ class RouteConfig { ); } path = pathValue.stringValue; + + final ConstantReader nameValue = reader.read('name'); + name = nameValue.isNull ? null : nameValue.stringValue; } final DartType typeParamType = type.typeArguments.single; @@ -104,6 +109,7 @@ class RouteConfig { final RouteConfig value = RouteConfig._( path ?? '', + name, classElement, parent, _generateNavigatorKeyGetterCode( @@ -121,6 +127,7 @@ class RouteConfig { final List _children = []; final String _path; + final String? _name; final InterfaceElement _routeDataClass; final RouteConfig? _parent; final String? _key; @@ -352,6 +359,7 @@ routes: [${_children.map((RouteConfig e) => '${e._routeDefinition()},').join()}] return ''' GoRouteData.\$route( path: ${escapeDartString(_path)}, + ${_name != null ? 'name: ${escapeDartString(_name!)},' : ''} factory: $_extensionName._fromState, $navigatorKey $routesBit diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml index 1f8b7b89f9b49..a8ae680e97a58 100644 --- a/packages/go_router_builder/pubspec.yaml +++ b/packages/go_router_builder/pubspec.yaml @@ -2,7 +2,7 @@ name: go_router_builder description: >- A builder that supports generated strongly-typed route helpers for package:go_router -version: 2.0.0 +version: 2.0.1 repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22 diff --git a/packages/go_router_builder/test/builder_test.dart b/packages/go_router_builder/test/builder_test.dart index e7d2b1440bcc6..056d854a4c003 100644 --- a/packages/go_router_builder/test/builder_test.dart +++ b/packages/go_router_builder/test/builder_test.dart @@ -37,4 +37,6 @@ const Set _expectedAnnotatedTests = { 'NullableDefaultValueRoute', 'IterableWithEnumRoute', 'IterableDefaultValueRoute', + 'NamedRoute', + 'NamedEscapedRoute', }; diff --git a/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart b/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart index 39b4add4fb9e6..678a5286e0886 100644 --- a/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart +++ b/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart @@ -327,3 +327,54 @@ class IterableDefaultValueRoute extends GoRouteData { IterableDefaultValueRoute({this.param = const [0]}); final Iterable param; } + +@ShouldGenerate(r''' +RouteBase get $namedRoute => GoRouteData.$route( + path: '/named-route', + name: 'namedRoute', + factory: $NamedRouteExtension._fromState, + ); + +extension $NamedRouteExtension on NamedRoute { + static NamedRoute _fromState(GoRouterState state) => NamedRoute(); + + String get location => GoRouteData.$location( + '/named-route', + ); + + void go(BuildContext context) => context.go(location); + + Future push(BuildContext context) => context.push(location); + + void pushReplacement(BuildContext context) => + context.pushReplacement(location); +} +''') +@TypedGoRoute(path: '/named-route', name: 'namedRoute') +class NamedRoute extends GoRouteData {} + +@ShouldGenerate(r''' +RouteBase get $namedEscapedRoute => GoRouteData.$route( + path: '/named-route', + name: r'named$Route', + factory: $NamedEscapedRouteExtension._fromState, + ); + +extension $NamedEscapedRouteExtension on NamedEscapedRoute { + static NamedEscapedRoute _fromState(GoRouterState state) => + NamedEscapedRoute(); + + String get location => GoRouteData.$location( + '/named-route', + ); + + void go(BuildContext context) => context.go(location); + + Future push(BuildContext context) => context.push(location); + + void pushReplacement(BuildContext context) => + context.pushReplacement(location); +} +''') +@TypedGoRoute(path: '/named-route', name: r'named$Route') +class NamedEscapedRoute extends GoRouteData {}