Skip to content

Commit 6328cfb

Browse files
authored
[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](#120102)
1 parent aa1dda5 commit 6328cfb

File tree

7 files changed

+67
-1
lines changed

7 files changed

+67
-1
lines changed

packages/go_router_builder/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.0.1
2+
3+
* Supports name parameter for `TypedGoRoute`.
14
## 2.0.0
25

36
* Updates the documentation to go_router v7.0.0.

packages/go_router_builder/example/lib/simple_example.dart

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class App extends StatelessWidget {
2828

2929
@TypedGoRoute<HomeRoute>(
3030
path: '/',
31+
name: 'Home',
3132
routes: <TypedGoRoute<GoRouteData>>[
3233
TypedGoRoute<FamilyRoute>(path: 'family/:familyId')
3334
],

packages/go_router_builder/example/lib/simple_example.g.dart

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/go_router_builder/lib/src/route_config.dart

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class InfoIterable extends IterableBase<String> {
3737
class RouteConfig {
3838
RouteConfig._(
3939
this._path,
40+
this._name,
4041
this._routeDataClass,
4142
this._parent,
4243
this._key,
@@ -75,6 +76,7 @@ class RouteConfig {
7576
final bool isShellRoute = type.element.name == 'TypedShellRoute';
7677

7778
String? path;
79+
String? name;
7880

7981
if (!isShellRoute) {
8082
final ConstantReader pathValue = reader.read('path');
@@ -85,6 +87,9 @@ class RouteConfig {
8587
);
8688
}
8789
path = pathValue.stringValue;
90+
91+
final ConstantReader nameValue = reader.read('name');
92+
name = nameValue.isNull ? null : nameValue.stringValue;
8893
}
8994

9095
final DartType typeParamType = type.typeArguments.single;
@@ -104,6 +109,7 @@ class RouteConfig {
104109

105110
final RouteConfig value = RouteConfig._(
106111
path ?? '',
112+
name,
107113
classElement,
108114
parent,
109115
_generateNavigatorKeyGetterCode(
@@ -121,6 +127,7 @@ class RouteConfig {
121127

122128
final List<RouteConfig> _children = <RouteConfig>[];
123129
final String _path;
130+
final String? _name;
124131
final InterfaceElement _routeDataClass;
125132
final RouteConfig? _parent;
126133
final String? _key;
@@ -352,6 +359,7 @@ routes: [${_children.map((RouteConfig e) => '${e._routeDefinition()},').join()}]
352359
return '''
353360
GoRouteData.\$route(
354361
path: ${escapeDartString(_path)},
362+
${_name != null ? 'name: ${escapeDartString(_name!)},' : ''}
355363
factory: $_extensionName._fromState,
356364
$navigatorKey
357365
$routesBit

packages/go_router_builder/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: go_router_builder
22
description: >-
33
A builder that supports generated strongly-typed route helpers for
44
package:go_router
5-
version: 2.0.0
5+
version: 2.0.1
66
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
77
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22
88

packages/go_router_builder/test/builder_test.dart

+2
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ const Set<String> _expectedAnnotatedTests = <String>{
3737
'NullableDefaultValueRoute',
3838
'IterableWithEnumRoute',
3939
'IterableDefaultValueRoute',
40+
'NamedRoute',
41+
'NamedEscapedRoute',
4042
};

packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart

+51
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,54 @@ class IterableDefaultValueRoute extends GoRouteData {
327327
IterableDefaultValueRoute({this.param = const <int>[0]});
328328
final Iterable<int> param;
329329
}
330+
331+
@ShouldGenerate(r'''
332+
RouteBase get $namedRoute => GoRouteData.$route(
333+
path: '/named-route',
334+
name: 'namedRoute',
335+
factory: $NamedRouteExtension._fromState,
336+
);
337+
338+
extension $NamedRouteExtension on NamedRoute {
339+
static NamedRoute _fromState(GoRouterState state) => NamedRoute();
340+
341+
String get location => GoRouteData.$location(
342+
'/named-route',
343+
);
344+
345+
void go(BuildContext context) => context.go(location);
346+
347+
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
348+
349+
void pushReplacement(BuildContext context) =>
350+
context.pushReplacement(location);
351+
}
352+
''')
353+
@TypedGoRoute<NamedRoute>(path: '/named-route', name: 'namedRoute')
354+
class NamedRoute extends GoRouteData {}
355+
356+
@ShouldGenerate(r'''
357+
RouteBase get $namedEscapedRoute => GoRouteData.$route(
358+
path: '/named-route',
359+
name: r'named$Route',
360+
factory: $NamedEscapedRouteExtension._fromState,
361+
);
362+
363+
extension $NamedEscapedRouteExtension on NamedEscapedRoute {
364+
static NamedEscapedRoute _fromState(GoRouterState state) =>
365+
NamedEscapedRoute();
366+
367+
String get location => GoRouteData.$location(
368+
'/named-route',
369+
);
370+
371+
void go(BuildContext context) => context.go(location);
372+
373+
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
374+
375+
void pushReplacement(BuildContext context) =>
376+
context.pushReplacement(location);
377+
}
378+
''')
379+
@TypedGoRoute<NamedEscapedRoute>(path: '/named-route', name: r'named$Route')
380+
class NamedEscapedRoute extends GoRouteData {}

0 commit comments

Comments
 (0)