[go_router_builder] Adds name parameter to TypedGoRoute and supports its generation. (#3665)
This PR allows for generating `TypedGoRoutes` by defining a custom name and forwarding it to the underlying `GoRoute` this is needed when working with Analytics services such as Google analytics that use the name to log the page in their systems.
Fixes [#120102](https://github.com/flutter/flutter/issues/120102)
diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md
index 304b579..a1c74c7 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 6253fc6..dee29d0 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 @@
@TypedGoRoute<HomeRoute>(
path: '/',
+ name: 'Home',
routes: <TypedGoRoute<GoRouteData>>[
TypedGoRoute<FamilyRoute>(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 410c914..d3e624d 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 @@
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 50d7e77..a67f33b 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 RouteConfig {
RouteConfig._(
this._path,
+ this._name,
this._routeDataClass,
this._parent,
this._key,
@@ -75,6 +76,7 @@
final bool isShellRoute = type.element.name == 'TypedShellRoute';
String? path;
+ String? name;
if (!isShellRoute) {
final ConstantReader pathValue = reader.read('path');
@@ -85,6 +87,9 @@
);
}
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 @@
final RouteConfig value = RouteConfig._(
path ?? '',
+ name,
classElement,
parent,
_generateNavigatorKeyGetterCode(
@@ -121,6 +127,7 @@
final List<RouteConfig> _children = <RouteConfig>[];
final String _path;
+ final String? _name;
final InterfaceElement _routeDataClass;
final RouteConfig? _parent;
final String? _key;
@@ -352,6 +359,7 @@
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 1f8b7b8..a8ae680 100644
--- a/packages/go_router_builder/pubspec.yaml
+++ b/packages/go_router_builder/pubspec.yaml
@@ -2,7 +2,7 @@
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 e7d2b14..056d854 100644
--- a/packages/go_router_builder/test/builder_test.dart
+++ b/packages/go_router_builder/test/builder_test.dart
@@ -37,4 +37,6 @@
'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 39b4add..678a528 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 @@
IterableDefaultValueRoute({this.param = const <int>[0]});
final Iterable<int> 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<T?> push<T>(BuildContext context) => context.push<T>(location);
+
+ void pushReplacement(BuildContext context) =>
+ context.pushReplacement(location);
+}
+''')
+@TypedGoRoute<NamedRoute>(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<T?> push<T>(BuildContext context) => context.push<T>(location);
+
+ void pushReplacement(BuildContext context) =>
+ context.pushReplacement(location);
+}
+''')
+@TypedGoRoute<NamedEscapedRoute>(path: '/named-route', name: r'named$Route')
+class NamedEscapedRoute extends GoRouteData {}