-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[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
Open
ThangVuNguyenViet
wants to merge
26
commits into
flutter:main
Choose a base branch
from
ThangVuNguyenViet:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
37be48c
- Adds `GoRouter.goRelative`
ThangVuNguyenViet f8061ce
add go_relative example for go_router
ThangVuNguyenViet 8a77147
add go_relative_test for the example
ThangVuNguyenViet 61127f3
fix failed test
ThangVuNguyenViet 687b332
replace goRelative with go('./$path')
ThangVuNguyenViet cca1454
Commit missing files during merge
ThangVuNguyenViet 975128b
update changelog & version
ThangVuNguyenViet 6d90488
Prevent concatenateUris from adding trailing redundant '?'. Add test …
ThangVuNguyenViet ccb38f9
Add more `concatenateUris` test. Fix joining params
ThangVuNguyenViet 80d85d6
update example description
ThangVuNguyenViet a09aa94
Make concatenateUris not merging parameters, only take them from chil…
ThangVuNguyenViet a174cbe
Add GoRelativeRouteConfig
ThangVuNguyenViet 4cb0334
Add test, examples and example tests for go_router_builder. Temporari…
ThangVuNguyenViet ceaa318
Add relativeLocation, push, pushReplacement & replace to the Relative…
ThangVuNguyenViet d3b5355
Merge branch 'flutter-main'
ThangVuNguyenViet 07b4a23
update go_router package dependency
ThangVuNguyenViet 6008b8b
Fix merge issues
ThangVuNguyenViet d2fbc0e
temporarily add dependency_override
ThangVuNguyenViet 8b5fb4d
change relative route actions to have suffix `Relative`
ThangVuNguyenViet 02c3d7b
Fix example
ThangVuNguyenViet 72c06de
Update go_relative test
ThangVuNguyenViet edef7a5
Merge branch 'main' of github.com:flutter/packages
ThangVuNguyenViet 5474303
fix generated file
ThangVuNguyenViet dbf7f9d
Fix failed test due to generated files
ThangVuNguyenViet 37c5490
Merge branch 'main' of github.com:flutter/packages
ThangVuNguyenViet 868a2d5
Merge branch 'main' of github.com:flutter/packages
ThangVuNguyenViet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
packages/go_router_builder/example/lib/go_relative.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// ignore_for_file: public_member_api_docs, unreachable_from_main | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
part 'go_relative.g.dart'; | ||
|
||
void main() => runApp(const MyApp()); | ||
|
||
/// The main app. | ||
class MyApp extends StatelessWidget { | ||
/// Constructs a [MyApp] | ||
const MyApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp.router( | ||
routerConfig: _router, | ||
); | ||
} | ||
} | ||
|
||
/// The route configuration. | ||
final GoRouter _router = GoRouter( | ||
routes: $appRoutes, | ||
); | ||
const TypedRelativeGoRoute<DetailsRoute> detailRoute = | ||
TypedRelativeGoRoute<DetailsRoute>( | ||
path: 'details/:detailId', | ||
routes: <TypedRoute<RouteData>>[ | ||
TypedRelativeGoRoute<SettingsRoute>(path: 'settings/:settingId'), | ||
], | ||
); | ||
|
||
@TypedGoRoute<HomeRoute>( | ||
path: '/', | ||
routes: <TypedRoute<RouteData>>[ | ||
TypedGoRoute<DashboardRoute>( | ||
path: '/dashboard', | ||
routes: <TypedRoute<RouteData>>[detailRoute], | ||
), | ||
detailRoute, | ||
], | ||
) | ||
class HomeRoute extends GoRouteData { | ||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return const HomeScreen(); | ||
} | ||
} | ||
|
||
class DashboardRoute extends GoRouteData { | ||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return const DashboardScreen(); | ||
} | ||
} | ||
|
||
class DetailsRoute extends GoRouteData { | ||
const DetailsRoute({required this.detailId}); | ||
final String detailId; | ||
|
||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return DetailsScreen(id: detailId); | ||
} | ||
} | ||
|
||
class SettingsRoute extends GoRouteData { | ||
const SettingsRoute({ | ||
required this.settingId, | ||
}); | ||
final String settingId; | ||
|
||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return SettingsScreen(id: settingId); | ||
} | ||
} | ||
|
||
/// The home screen | ||
class HomeScreen extends StatelessWidget { | ||
/// Constructs a [HomeScreen] | ||
const HomeScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Home Screen')), | ||
body: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
ElevatedButton( | ||
onPressed: () { | ||
const DetailsRoute(detailId: 'DetailsId').goRelative(context); | ||
}, | ||
child: const Text('Go to the Details screen'), | ||
), | ||
ElevatedButton( | ||
onPressed: () { | ||
DashboardRoute().go(context); | ||
}, | ||
child: const Text('Go to the Dashboard screen'), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// The home screen | ||
class DashboardScreen extends StatelessWidget { | ||
/// Constructs a [DashboardScreen] | ||
const DashboardScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Dashboard Screen')), | ||
body: Column( | ||
children: <Widget>[ | ||
ElevatedButton( | ||
onPressed: () { | ||
const DetailsRoute(detailId: 'DetailsId').goRelative(context); | ||
}, | ||
child: const Text('Go to the Details screen'), | ||
), | ||
ElevatedButton( | ||
onPressed: () => context.pop(), | ||
child: const Text('Go back'), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// The details screen | ||
class DetailsScreen extends StatelessWidget { | ||
/// Constructs a [DetailsScreen] | ||
const DetailsScreen({ | ||
super.key, | ||
required this.id, | ||
}); | ||
|
||
final String id; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: Text('Details Screen $id')), | ||
body: Center( | ||
child: Column( | ||
children: <Widget>[ | ||
ElevatedButton( | ||
onPressed: () => context.pop(), | ||
child: const Text('Go back'), | ||
), | ||
ElevatedButton( | ||
onPressed: () => const SettingsRoute( | ||
settingId: 'SettingsId', | ||
).goRelative(context), | ||
child: const Text('Go to the Settings screen'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// The details screen | ||
class SettingsScreen extends StatelessWidget { | ||
/// Constructs a [SettingsScreen] | ||
const SettingsScreen({ | ||
super.key, | ||
required this.id, | ||
}); | ||
|
||
final String id; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: Text('Settings Screen $id')), | ||
body: Center( | ||
child: TextButton( | ||
onPressed: () => context.pop(), | ||
child: const Text('Go back'), | ||
), | ||
), | ||
); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
packages/go_router_builder/example/lib/go_relative.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is different between using this vs a regular TypedGoRoute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example I gave isn't clearly explaining the purpose. Let me update this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still can't see the difference. Is this a way to reuse the typedGoRoute in multiple places in the routing tree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Without this you'd have to define routes like
SettingsFromHomeRoute
SettingsFromDashboardRoute
And those routes all build (or build page) the same screen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah this makes sense. I will take another look