Skip to content
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

[go _route] fragment parameter added #8232

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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 @@
## 14.7.0

- Adds fragment support to GoRouter, enabling direct specification and automatic handling of fragments in routes.

## 14.6.2

- Replaces deprecated collection method usage.
Expand Down
17 changes: 10 additions & 7 deletions packages/go_router/lib/src/misc/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ extension GoRouterHelper on BuildContext {
String name, {
Map<String, String> pathParameters = const <String, String>{},
Map<String, dynamic> queryParameters = const <String, dynamic>{},
String? fragment,
}) =>
GoRouter.of(this).namedLocation(name,
pathParameters: pathParameters, queryParameters: queryParameters);
pathParameters: pathParameters,
queryParameters: queryParameters,
fragment: fragment);

/// Navigate to a location.
void go(String location, {Object? extra}) =>
Expand All @@ -30,13 +33,13 @@ extension GoRouterHelper on BuildContext {
Map<String, String> pathParameters = const <String, String>{},
Map<String, dynamic> queryParameters = const <String, dynamic>{},
Object? extra,
String? fragment,
}) =>
GoRouter.of(this).goNamed(
name,
pathParameters: pathParameters,
queryParameters: queryParameters,
extra: extra,
);
GoRouter.of(this).goNamed(name,
pathParameters: pathParameters,
queryParameters: queryParameters,
extra: extra,
fragment: fragment);

/// Push a location onto the page stack.
///
Expand Down
19 changes: 12 additions & 7 deletions packages/go_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,16 @@ class GoRouter implements RouterConfig<RouteMatchList> {

/// Get a location from route name and parameters.
/// This is useful for redirecting to a named location.
String namedLocation(
String name, {
Map<String, String> pathParameters = const <String, String>{},
Map<String, dynamic> queryParameters = const <String, dynamic>{},
}) =>
String namedLocation(String name,
{Map<String, String> pathParameters = const <String, String>{},
Map<String, dynamic> queryParameters = const <String, dynamic>{},
String? fragment}) =>
configuration.namedLocation(
name,
pathParameters: pathParameters,
queryParameters: queryParameters,
);
) +
AffanShaikhsurab marked this conversation as resolved.
Show resolved Hide resolved
((fragment?.isNotEmpty ?? false) ? '#$fragment' : '');

/// Navigate to a URI location w/ optional query parameters, e.g.
/// `/family/f2/person/p1?color=blue`
Expand All @@ -366,10 +366,15 @@ class GoRouter implements RouterConfig<RouteMatchList> {
Map<String, String> pathParameters = const <String, String>{},
Map<String, dynamic> queryParameters = const <String, dynamic>{},
Object? extra,
String? fragment,
}) =>

/// Construct location with optional fragment, using null-safe navigation
go(
namedLocation(name,
pathParameters: pathParameters, queryParameters: queryParameters),
pathParameters: pathParameters,
queryParameters: queryParameters) +
((fragment?.isNotEmpty ?? false) ? '#$fragment' : ''),
extra: extra,
);

Expand Down
6 changes: 5 additions & 1 deletion packages/go_router/lib/src/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ class GoRouterState {
String name, {
Map<String, String> pathParameters = const <String, String>{},
Map<String, String> queryParameters = const <String, String>{},
String? fragment,
}) {
// Generate base location using configuration, with optional path and query parameters
// Then conditionally append fragment if it exists and is not empty
return _configuration.namedLocation(name,
pathParameters: pathParameters, queryParameters: queryParameters);
pathParameters: pathParameters, queryParameters: queryParameters) +
((fragment?.isNotEmpty ?? false) ? '#$fragment' : '');
AffanShaikhsurab marked this conversation as resolved.
Show resolved Hide resolved
}

@override
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 14.6.2
version: 14.7.0
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down
36 changes: 36 additions & 0 deletions packages/go_router/test/go_route_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,42 @@ void main() {
expect(tester.takeException(), isAssertionError);
});

testWidgets('throw if redirect to itself.', (WidgetTester tester) async {
final GoRouter router = await createRouter(
<RouteBase>[
GoRoute(
path: '/',
builder: (_, __) => const Text('home'),
routes: <RouteBase>[
GoRoute(
path: 'route',
name: 'route', // Named route
redirect: (_, __) => '/route',
routes: <RouteBase>[
GoRoute(
path: '1',
builder: (_, __) => const Text('/route/1/2'),
),
],
),
],
),
],
tester,
);
expect(find.text('home'), findsOneWidget);

// Test namedLocation with fragment
final String locationWithFragment =
router.namedLocation('route', fragment: '2');
expect(locationWithFragment, '/route#2');
// Navigate using goNamed with fragment
router.goNamed('route', fragment: '2');
await tester.pumpAndSettle();
// Should redirect to /route/1 without error.
AffanShaikhsurab marked this conversation as resolved.
Show resolved Hide resolved
expect(tester.takeException(), isAssertionError);
AffanShaikhsurab marked this conversation as resolved.
Show resolved Hide resolved
});

testWidgets('throw if sub route does not conform with parent navigator key',
(WidgetTester tester) async {
final GlobalKey<NavigatorState> key1 = GlobalKey<NavigatorState>();
Expand Down
14 changes: 9 additions & 5 deletions packages/go_router/test/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ class GoRouterNamedLocationSpy extends GoRouter {
String? name;
Map<String, String>? pathParameters;
Map<String, dynamic>? queryParameters;
String? fragment;

@override
String namedLocation(
String name, {
Map<String, String> pathParameters = const <String, String>{},
Map<String, dynamic> queryParameters = const <String, dynamic>{},
}) {
String namedLocation(String name,
{Map<String, String> pathParameters = const <String, String>{},
Map<String, dynamic> queryParameters = const <String, dynamic>{},
String? fragment}) {
this.name = name;
this.pathParameters = pathParameters;
this.queryParameters = queryParameters;
this.fragment = fragment;
return '';
}
}
Expand Down Expand Up @@ -85,18 +86,21 @@ class GoRouterGoNamedSpy extends GoRouter {
Map<String, String>? pathParameters;
Map<String, dynamic>? queryParameters;
Object? extra;
String? fragment;

@override
void goNamed(
String name, {
Map<String, String> pathParameters = const <String, String>{},
Map<String, dynamic> queryParameters = const <String, dynamic>{},
Object? extra,
String? fragment,
}) {
this.name = name;
this.pathParameters = pathParameters;
this.queryParameters = queryParameters;
this.extra = extra;
this.fragment = fragment;
}
}

Expand Down
Loading