Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 34 additions & 0 deletions packages/go_router/example/lib/path_parameter_regex.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2013 The Flutter Authors
// 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';
Comment thread
saurabh-mirajkar marked this conversation as resolved.
import 'package:go_router/go_router.dart';

/// Router configuration demonstrating regex-constrained path parameters.
final GoRouter router = GoRouter(
routes: <GoRoute>[
GoRoute(
path: r'/users/:id(\d+)',
builder: (BuildContext context, GoRouterState state) {
return Scaffold(body: Center(child: Text('User ${state.pathParameters['id']}')));
},
),
],
);

/// Runs the path parameter regular expression example.
void main() {
Comment thread
saurabh-mirajkar marked this conversation as resolved.
runApp(const PathParameterRegexApp());
}

/// A minimal app demonstrating regex-constrained path parameters.
class PathParameterRegexApp extends StatelessWidget {
/// Creates a [PathParameterRegexApp].
const PathParameterRegexApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}
17 changes: 17 additions & 0 deletions packages/go_router/example/test/path_parameter_regex_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2013 The Flutter Authors
// 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';
Comment thread
saurabh-mirajkar marked this conversation as resolved.
import 'package:go_router_examples/path_parameter_regex.dart' as example;

void main() {
testWidgets('matches numeric path parameter', (WidgetTester tester) async {
example.router.go('/users/42');

await tester.pumpWidget(const example.PathParameterRegexApp());
await tester.pumpAndSettle();

expect(find.text('User 42'), findsOneWidget);
});
}
15 changes: 15 additions & 0 deletions packages/go_router/lib/src/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: doc_directive_unknown

import 'dart:async';

import 'package:collection/collection.dart';
Expand Down Expand Up @@ -341,6 +343,19 @@ class GoRoute extends RouteBase {
/// `/family/456` and etc. The parameter values are stored in [GoRouterState]
/// that are passed into [pageBuilder] and [builder].
///
/// A path parameter may optionally be constrained to a regular expression
/// by appending the expression in parentheses after the parameter name:
/// `:paramName(regex)`.
///
/// {@example /example/lib/path_parameter_regex.dart}
///
/// The path matches `/users/42` but not `/users/settings`. If a path segment
/// does not satisfy the regular expression, route matching continues with
/// other route candidates.
///
/// The regular expression is interpreted as a Dart [RegExp] pattern and must
/// not contain nested parentheses.
///
/// The query parameter are also capture during the route parsing and stored
/// in [GoRouterState].
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog: |
- Documents support for regular expression constraints in GoRoute path parameters.
version: patch
Loading