Skip to content

Commit

Permalink
[go_router_builder] Adds name parameter to TypedGoRoute and supports …
Browse files Browse the repository at this point in the history
…its generation. (flutter#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 [flutter#120102](flutter#120102)
  • Loading branch information
dancamdev authored May 10, 2023
1 parent aa1dda5 commit 6328cfb
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.0.1

* Supports name parameter for `TypedGoRoute`.
## 2.0.0

* Updates the documentation to go_router v7.0.0.
Expand Down
1 change: 1 addition & 0 deletions packages/go_router_builder/example/lib/simple_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class App extends StatelessWidget {

@TypedGoRoute<HomeRoute>(
path: '/',
name: 'Home',
routes: <TypedGoRoute<GoRouteData>>[
TypedGoRoute<FamilyRoute>(path: 'family/:familyId')
],
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/go_router_builder/lib/src/route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class InfoIterable extends IterableBase<String> {
class RouteConfig {
RouteConfig._(
this._path,
this._name,
this._routeDataClass,
this._parent,
this._key,
Expand Down Expand Up @@ -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');
Expand All @@ -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;
Expand All @@ -104,6 +109,7 @@ class RouteConfig {

final RouteConfig value = RouteConfig._(
path ?? '',
name,
classElement,
parent,
_generateNavigatorKeyGetterCode(
Expand All @@ -121,6 +127,7 @@ class RouteConfig {

final List<RouteConfig> _children = <RouteConfig>[];
final String _path;
final String? _name;
final InterfaceElement _routeDataClass;
final RouteConfig? _parent;
final String? _key;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions packages/go_router_builder/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ const Set<String> _expectedAnnotatedTests = <String>{
'NullableDefaultValueRoute',
'IterableWithEnumRoute',
'IterableDefaultValueRoute',
'NamedRoute',
'NamedEscapedRoute',
};
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,54 @@ class IterableDefaultValueRoute extends GoRouteData {
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 {}

0 comments on commit 6328cfb

Please sign in to comment.