Skip to content

Commit

Permalink
replace goRelative with go('./$path')
Browse files Browse the repository at this point in the history
  • Loading branch information
ThangVuNguyenViet committed Jun 19, 2024
1 parent c1c1b93 commit 9aa61bd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 14.1.5

- Adds `GoRouter.goRelative`
- Allows going to a path relatively by prefixing `./`
- Adds `TypedRelativeGoRoute`

## 14.1.4
Expand Down
4 changes: 2 additions & 2 deletions packages/go_router/example/lib/go_relative.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class HomeScreen extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () => context.goRelative('details'),
onPressed: () => context.go('./details'),
child: const Text('Go to the Details screen'),
),
],
Expand Down Expand Up @@ -93,7 +93,7 @@ class DetailsScreen extends StatelessWidget {
),
TextButton(
onPressed: () {
context.goRelative('settings');
context.go('./settings');
},
child: const Text('Go to the Settings screen'),
),
Expand Down
10 changes: 8 additions & 2 deletions packages/go_router/lib/src/information_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

import 'match.dart';
import 'path_utils.dart';

/// The type of the navigation.
///
Expand Down Expand Up @@ -135,11 +136,16 @@ class GoRouteInformationProvider extends RouteInformationProvider
}

void _setValue(String location, Object state) {
final Uri uri = Uri.parse(location);
Uri uri = Uri.parse(location);

// Check for relative location
if (location.startsWith('./')) {
uri = concatenateUris(_value.uri, uri);
}

final bool shouldNotify =
_valueHasChanged(newLocationUri: uri, newState: state);
_value = RouteInformation(uri: Uri.parse(location), state: state);
_value = RouteInformation(uri: uri, state: state);
if (shouldNotify) {
notifyListeners();
}
Expand Down
7 changes: 0 additions & 7 deletions packages/go_router/lib/src/misc/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ extension GoRouterHelper on BuildContext {
void go(String location, {Object? extra}) =>
GoRouter.of(this).go(location, extra: extra);

/// Navigate relative to a location.
void goRelative(String location, {Object? extra}) =>
GoRouter.of(this).goRelative(
location,
extra: extra,
);

/// Navigate to a named route.
void goNamed(
String name, {
Expand Down
9 changes: 0 additions & 9 deletions packages/go_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,6 @@ class GoRouter implements RouterConfig<RouteMatchList> {
routeInformationProvider.go(location, extra: extra);
}

/// Navigate to a URI location by appending [relativeLocation] to the current [GoRouterState.matchedLocation] w/ optional query parameters, e.g.
void goRelative(
String relativeLocation, {
Object? extra,
}) {
log('going relative to $relativeLocation');
routeInformationProvider.goRelative(relativeLocation, extra: extra);
}

/// Restore the RouteMatchList
void restore(RouteMatchList matchList) {
log('restoring ${matchList.uri}');
Expand Down
21 changes: 11 additions & 10 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,7 @@ void main() {
];

final GoRouter router = await createRouter(routes, tester);
router.goRelative('login');
router.go('./login');
await tester.pumpAndSettle();
expect(find.byType(LoginScreen), findsOneWidget);
});
Expand All @@ -1832,7 +1832,7 @@ void main() {

final GoRouter router = await createRouter(routes, tester);
router.go('/home');
router.goRelative('login');
router.go('./login');
await tester.pumpAndSettle();
expect(find.byType(LoginScreen), findsOneWidget);
});
Expand Down Expand Up @@ -1870,11 +1870,11 @@ void main() {
final GoRouter router =
await createRouter(routes, tester, initialLocation: '/home');

router.goRelative('family/$fid');
router.go('./family/$fid');
await tester.pumpAndSettle();
expect(find.byType(FamilyScreen), findsOneWidget);

router.goRelative('person/$pid');
router.go('./person/$pid');
await tester.pumpAndSettle();
expect(find.byType(PersonScreen), findsOneWidget);
});
Expand Down Expand Up @@ -1911,11 +1911,11 @@ void main() {
final GoRouter router =
await createRouter(routes, tester, initialLocation: '/home');

router.goRelative('family?fid=$fid');
router.go('./family?fid=$fid');
await tester.pumpAndSettle();
expect(find.byType(FamilyScreen), findsOneWidget);

router.goRelative('person?pid=$pid');
router.go('./person?pid=$pid');
await tester.pumpAndSettle();
expect(find.byType(PersonScreen), findsOneWidget);
});
Expand Down Expand Up @@ -1952,7 +1952,7 @@ void main() {
errorBuilder: (BuildContext context, GoRouterState state) =>
TestErrorScreen(state.error!),
);
router.goRelative('family/person/$pid');
router.go('./family/person/$pid');
await tester.pumpAndSettle();
expect(find.byType(TestErrorScreen), findsOneWidget);

Expand Down Expand Up @@ -2023,7 +2023,7 @@ void main() {
final GoRouter router =
await createRouter(routes, tester, initialLocation: '/home');
final String loc = 'page1/${Uri.encodeComponent(param1)}';
router.goRelative(loc);
router.go('./$loc');

await tester.pumpAndSettle();
expect(find.byType(DummyScreen), findsOneWidget);
Expand Down Expand Up @@ -2054,10 +2054,11 @@ void main() {
final GoRouter router =
await createRouter(routes, tester, initialLocation: '/home');

router.goRelative(Uri(
final String loc = Uri(
path: 'page1',
queryParameters: <String, dynamic>{'param1': param1},
).toString());
).toString();
router.go('./$loc');

await tester.pumpAndSettle();
expect(find.byType(DummyScreen), findsOneWidget);
Expand Down

0 comments on commit 9aa61bd

Please sign in to comment.