-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[go_router] Add support for relative routes #6825
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
Merged
auto-submit
merged 20 commits into
flutter:main
from
ThangVuNguyenViet:go_router/go-relative
Nov 13, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
02689ff
- Adds `GoRouter.goRelative`
ThangVuNguyenViet e8ac4ee
add go_relative example for go_router
ThangVuNguyenViet c42151c
add go_relative_test for the example
ThangVuNguyenViet 022db98
fix failed test
ThangVuNguyenViet 812498e
replace goRelative with go('./$path')
ThangVuNguyenViet 07b63ad
Commit missing files during merge
ThangVuNguyenViet c025d86
update changelog & version
ThangVuNguyenViet 3fb6463
Prevent concatenateUris from adding trailing redundant '?'. Add test …
ThangVuNguyenViet 205d096
Add more `concatenateUris` test. Fix joining params
ThangVuNguyenViet 8e4db8e
update example description
ThangVuNguyenViet 0ead0af
Make concatenateUris not merging parameters, only take them from chil…
ThangVuNguyenViet 9407200
Refactor example & its test
ThangVuNguyenViet 6830539
Remove TypedRelativeGoRoute
ThangVuNguyenViet 43eb497
Add missing import
ThangVuNguyenViet 2dd80d9
add fragment test to concatenateUris
ThangVuNguyenViet 3071a53
Merge branch 'main' into go_router/go-relative
ThangVuNguyenViet 4dba574
Merge branch 'main' of github.com:flutter/packages into go_router/go-…
ThangVuNguyenViet fb61d97
Merge branch 'main' into go_router/go-relative
ThangVuNguyenViet d1ebfeb
bump version to 14.4.2 in pubspec.yaml
ThangVuNguyenViet c246bc3
Merge branch 'main' into go_router/go-relative
chunhtai 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// 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. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
/// This sample app demonstrates how to use go relatively with GoRouter.go('./$path'). | ||
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: <RouteBase>[ | ||
GoRoute( | ||
path: '/', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const HomeScreen(); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const DetailsScreen(); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'settings', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const SettingsScreen(); | ||
}, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
); | ||
|
||
/// 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: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
ElevatedButton( | ||
onPressed: () => context.go('./details'), | ||
child: const Text('Go to the Details screen'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// The details screen | ||
class DetailsScreen extends StatelessWidget { | ||
/// Constructs a [DetailsScreen] | ||
const DetailsScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Details Screen')), | ||
body: Center( | ||
child: Column( | ||
children: <Widget>[ | ||
TextButton( | ||
onPressed: () { | ||
context.pop(); | ||
}, | ||
child: const Text('Go back'), | ||
), | ||
TextButton( | ||
onPressed: () { | ||
context.go('./settings'); | ||
}, | ||
child: const Text('Go to the Settings screen'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// The settings screen | ||
class SettingsScreen extends StatelessWidget { | ||
/// Constructs a [SettingsScreen] | ||
const SettingsScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Settings Screen')), | ||
body: Column( | ||
children: <Widget>[ | ||
TextButton( | ||
onPressed: () { | ||
context.pop(); | ||
}, | ||
child: const Text('Go back'), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
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,29 @@ | ||
// 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. | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router_examples/go_relative.dart' as example; | ||
|
||
void main() { | ||
testWidgets('example works', (WidgetTester tester) async { | ||
await tester.pumpWidget(const example.MyApp()); | ||
expect(find.byType(example.HomeScreen), findsOneWidget); | ||
|
||
await tester.tap(find.text('Go to the Details screen')); | ||
await tester.pumpAndSettle(); | ||
expect(find.byType(example.DetailsScreen), findsOneWidget); | ||
|
||
await tester.tap(find.text('Go to the Settings screen')); | ||
await tester.pumpAndSettle(); | ||
expect(find.byType(example.SettingsScreen), findsOneWidget); | ||
|
||
await tester.tap(find.text('Go back')); | ||
await tester.pumpAndSettle(); | ||
expect(find.byType(example.DetailsScreen), findsOneWidget); | ||
|
||
await tester.tap(find.text('Go back')); | ||
await tester.pumpAndSettle(); | ||
expect(find.byType(example.HomeScreen), findsOneWidget); | ||
}); | ||
} |
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
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.
the path looks good, but I am a bit hesitate on also merge query parameter. It has different logic from regular go. Regular go will not attempt to merge with current query parameters. I think we should probably follow the same logic to avoid confusion.
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.
Hmm this is quite interesting thing thou. Take this example.
We're in
/family/f2?sort=desc
and we're going to the innerperson/p1
route, and we'll pop it to go back to thefamily
route. How are devs handling this case?In go_router_builder, devs can't use the easy
PersonRoute(pid: 'p1').go
but rather have to get itsroute.location
, convert to Uri, and then manually add the sort param. Being able to useRoute().go
in builder is kinda a big selling point of the package imoThere 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.
you can access GoRouterState.of(context).uri.queryParemeters.
Also won't in this case PersonRoute also declare
sort
parameter?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.
make sense. I've been doing things wrong in my app then