Skip to content

[go_router_builder] Add support for relative routes #8476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
37be48c
- Adds `GoRouter.goRelative`
ThangVuNguyenViet May 29, 2024
f8061ce
add go_relative example for go_router
ThangVuNguyenViet May 29, 2024
8a77147
add go_relative_test for the example
ThangVuNguyenViet May 29, 2024
61127f3
fix failed test
ThangVuNguyenViet May 29, 2024
687b332
replace goRelative with go('./$path')
ThangVuNguyenViet Jun 19, 2024
cca1454
Commit missing files during merge
ThangVuNguyenViet Jun 19, 2024
975128b
update changelog & version
ThangVuNguyenViet Jun 19, 2024
6d90488
Prevent concatenateUris from adding trailing redundant '?'. Add test …
ThangVuNguyenViet Jun 19, 2024
ccb38f9
Add more `concatenateUris` test. Fix joining params
ThangVuNguyenViet Jun 19, 2024
80d85d6
update example description
ThangVuNguyenViet Jul 10, 2024
a09aa94
Make concatenateUris not merging parameters, only take them from chil…
ThangVuNguyenViet Jul 19, 2024
a174cbe
Add GoRelativeRouteConfig
ThangVuNguyenViet Jul 10, 2024
4cb0334
Add test, examples and example tests for go_router_builder. Temporari…
ThangVuNguyenViet Jul 19, 2024
ceaa318
Add relativeLocation, push, pushReplacement & replace to the Relative…
ThangVuNguyenViet Jul 19, 2024
d3b5355
Merge branch 'flutter-main'
ThangVuNguyenViet Jan 22, 2025
07b4a23
update go_router package dependency
ThangVuNguyenViet Jan 22, 2025
6008b8b
Fix merge issues
ThangVuNguyenViet Jan 22, 2025
d2fbc0e
temporarily add dependency_override
ThangVuNguyenViet Jan 22, 2025
8b5fb4d
change relative route actions to have suffix `Relative`
ThangVuNguyenViet Feb 3, 2025
02c3d7b
Fix example
ThangVuNguyenViet Feb 4, 2025
72c06de
Update go_relative test
ThangVuNguyenViet Feb 6, 2025
edef7a5
Merge branch 'main' of github.com:flutter/packages
ThangVuNguyenViet May 17, 2025
5474303
fix generated file
ThangVuNguyenViet May 17, 2025
dbf7f9d
Fix failed test due to generated files
ThangVuNguyenViet May 17, 2025
37c5490
Merge branch 'main' of github.com:flutter/packages
ThangVuNguyenViet Jun 2, 2025
868a2d5
Merge branch 'main' of github.com:flutter/packages
ThangVuNguyenViet Jun 12, 2025
232dc7d
Merge branch 'main' of github.com:flutter/packages
ThangVuNguyenViet Jul 1, 2025
1d5ad11
Merge branch 'main' of github.com:flutter/packages
ThangVuNguyenViet Jul 9, 2025
f899939
Remove GoRouteData routing methods. Update TypedRelativeGoRoute gener…
ThangVuNguyenViet Jul 9, 2025
92f6cac
remove overrides on other tests
ThangVuNguyenViet Jul 9, 2025
f98505e
make sure relative go route generated code resembles typed go route a…
ThangVuNguyenViet Jul 9, 2025
20fd5d6
update changelog
ThangVuNguyenViet Jul 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 16.0.1

- Adds `TypedRelativeGoRoute`

## 16.0.0

- **BREAKING CHANGE**
Expand Down
58 changes: 34 additions & 24 deletions packages/go_router/lib/src/route_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,30 +133,6 @@ abstract class GoRouteData extends RouteData {
static final Expando<GoRouteData> _stateObjectExpando = Expando<GoRouteData>(
'GoRouteState to GoRouteData expando',
);

/// The location of this route.
String get location => throw _shouldBeGeneratedError;

/// Navigate to the route.
void go(BuildContext context) => throw _shouldBeGeneratedError;

/// Push the route onto the page stack.
Future<T?> push<T>(BuildContext context) => throw _shouldBeGeneratedError;

/// Replaces the top-most page of the page stack with the route.
void pushReplacement(BuildContext context) => throw _shouldBeGeneratedError;

/// Replaces the top-most page of the page stack with the route but treats
/// it as the same page.
///
/// The page key will be reused. This will preserve the state and not run any
/// page animation.
///
void replace(BuildContext context) => throw _shouldBeGeneratedError;

static UnimplementedError get _shouldBeGeneratedError => UnimplementedError(
'Should be generated using [Type-safe routing](https://pub.dev/documentation/go_router/latest/topics/Type-safe%20routes-topic.html).',
);
}

/// A class to represent a [ShellRoute] in
Expand Down Expand Up @@ -430,6 +406,40 @@ class TypedGoRoute<T extends GoRouteData> extends TypedRoute<T> {
final bool caseSensitive;
}

/// A superclass for each typed go route descendant
@Target(<TargetKind>{TargetKind.library, TargetKind.classType})
class TypedRelativeGoRoute<T extends GoRouteData> extends TypedRoute<T> {
/// Default const constructor
const TypedRelativeGoRoute({
required this.path,
this.routes = const <TypedRoute<RouteData>>[],
this.caseSensitive = true,
});

/// The relative path that corresponds to this route.
///
/// See [GoRoute.path].
///
///
final String path;

/// Child route definitions.
///
/// See [RouteBase.routes].
final List<TypedRoute<RouteData>> routes;

/// Determines whether the route matching is case sensitive.
///
/// When `true`, the path must match the specified case. For example,
/// a route with `path: '/family/:fid'` will not match `/FaMiLy/f2`.
///
/// When `false`, the path matching is case insensitive. The route
/// with `path: '/family/:fid'` will match `/FaMiLy/f2`.
///
/// Defaults to `true`.
final bool caseSensitive;
}

/// A superclass for each typed shell route descendant
@Target(<TargetKind>{TargetKind.library, TargetKind.classType})
class TypedShellRoute<T extends ShellRouteData> extends TypedRoute<T> {
Expand Down
70 changes: 0 additions & 70 deletions packages/go_router/test/route_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,76 +280,6 @@ void main() {
expect(routeWithDefaultCaseSensitivity.caseSensitive, false);
},
);

testWidgets(
'It should throw beacuase there is no code generated',
(WidgetTester tester) async {
final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];

FlutterError.onError =
(FlutterErrorDetails details) => errors.add(details);

const String errorText = 'Should be generated';

Widget buildWidget(void Function(BuildContext) onTap) {
return MaterialApp(
home: Builder(
builder: (BuildContext context) => GestureDetector(
child: const Text('Tap'),
onTap: () => onTap(context),
),
),
);
}

final Widget pushThrower = buildWidget((BuildContext context) {
const _GoRouteDataBuild().push<void>(context);
});
await tester.pumpWidget(pushThrower);
await tester.tap(find.text('Tap'));

expect(errors.first.exception, isA<UnimplementedError>());
expect(errors.first.exception.toString(), contains(errorText));

errors.clear();

final Widget goThrower = buildWidget((BuildContext context) {
const _GoRouteDataBuild().go(context);
});
await tester.pumpWidget(goThrower);
await tester.tap(find.text('Tap'));

expect(errors.first.exception, isA<UnimplementedError>());
expect(errors.first.exception.toString(), contains(errorText));

errors.clear();

final Widget pushReplacementThrower =
buildWidget((BuildContext context) {
const _GoRouteDataBuild().pushReplacement(context);
});
await tester.pumpWidget(pushReplacementThrower);
await tester.tap(find.text('Tap'));

expect(errors.first.exception, isA<UnimplementedError>());
expect(errors.first.exception.toString(), contains(errorText));

errors.clear();

final Widget replaceThrower = buildWidget((BuildContext context) {
const _GoRouteDataBuild().pushReplacement(context);
});
await tester.pumpWidget(replaceThrower);
await tester.tap(find.text('Tap'));

expect(errors.first.exception, isA<UnimplementedError>());
expect(errors.first.exception.toString(), contains(errorText));

errors.clear();

FlutterError.onError = FlutterError.dumpErrorToConsole;
},
);
});

group('ShellRouteData', () {
Expand Down
4 changes: 4 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

- Restricts `build` to versions less than 2.5.0.

## 3.0.2

- Adds `TypedRelativeGoRoute` annotation which supports relative routes.

## 3.0.1

- Updates README.md to use the mixin `with _$RouteName`.
Expand Down
Loading