diff --git a/packages/go_router/example/lib/path_parameter_regex.dart b/packages/go_router/example/lib/path_parameter_regex.dart new file mode 100644 index 000000000000..4951a09d98ab --- /dev/null +++ b/packages/go_router/example/lib/path_parameter_regex.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; + +/// Router configuration demonstrating regex-constrained path parameters. +final GoRouter router = GoRouter( + routes: [ + 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() { + runApp(MaterialApp.router(routerConfig: router)); +} diff --git a/packages/go_router/lib/src/route.dart b/packages/go_router/lib/src/route.dart index 6d75560a93cd..25389f368584 100644 --- a/packages/go_router/lib/src/route.dart +++ b/packages/go_router/lib/src/route.dart @@ -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'; @@ -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]. /// diff --git a/packages/go_router/pending_changelogs/change_2026_07_24_1784894419364.yaml b/packages/go_router/pending_changelogs/change_2026_07_24_1784894419364.yaml new file mode 100644 index 000000000000..826bd572d403 --- /dev/null +++ b/packages/go_router/pending_changelogs/change_2026_07_24_1784894419364.yaml @@ -0,0 +1,3 @@ +changelog: | + - Documents support for regular expression constraints in GoRoute path parameters. +version: patch