Skip to content

Commit

Permalink
[go_router_builder] Support default values for route parameters (flut…
Browse files Browse the repository at this point in the history
…ter#2826)

* ✨ Support default value in route parameters

* 📝 Update the example to include default value

* ✅ Update the tests

* ⬆️ Update the version number

* 🔊 Throw an error when a default value is used with a nullable type

* ✅ Add tests on default values

* 🚨 Specify type annotation

* 📝 Update README

* ✏️ equal -> that equals
  • Loading branch information
ValentinVignal authored Feb 8, 2023
1 parent 86ff76a commit f2f6c5e
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.0

* Supports default value for the route parameters.

## 1.0.16

* Update the documentation to go_router v6.0.0.
Expand Down
17 changes: 17 additions & 0 deletions packages/go_router_builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,23 @@ class LoginRoute extends GoRouteData {
}
```

### Default values

For query parameters with a **non-nullable** type, you can define a default value:

```dart
class MyRoute extends GoRouteData {
MyRoute({this.queryParameter = 'defaultValue'});
final String from;
@override
Widget build(BuildContext context, GoRouterState state) => MyScreen(from: from);
}
```

A query parameter that equals to its default value is not included in the location.


## Extra parameter

A route can consume an extra parameter by taking it as a typed constructor
Expand Down
26 changes: 26 additions & 0 deletions packages/go_router_builder/example/lib/all_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ class BoolRoute extends GoRouteData {
BoolRoute({
required this.requiredBoolField,
this.boolField,
this.boolFieldWithDefaultValue = true,
});

final bool requiredBoolField;
final bool? boolField;
final bool boolFieldWithDefaultValue;

@override
Widget build(BuildContext context, GoRouterState state) => BasePage<bool>(
dataTitle: 'BoolRoute',
param: requiredBoolField,
queryParam: boolField,
queryParamWithDefaultValue: boolFieldWithDefaultValue,
);

Widget drawerTile(BuildContext context) => ListTile(
Expand Down Expand Up @@ -110,16 +113,19 @@ class DoubleRoute extends GoRouteData {
DoubleRoute({
required this.requiredDoubleField,
this.doubleField,
this.doubleFieldWithDefaultValue = 1.0,
});

final double requiredDoubleField;
final double? doubleField;
final double doubleFieldWithDefaultValue;

@override
Widget build(BuildContext context, GoRouterState state) => BasePage<double>(
dataTitle: 'DoubleRoute',
param: requiredDoubleField,
queryParam: doubleField,
queryParamWithDefaultValue: doubleFieldWithDefaultValue,
);

Widget drawerTile(BuildContext context) => ListTile(
Expand All @@ -133,16 +139,19 @@ class IntRoute extends GoRouteData {
IntRoute({
required this.requiredIntField,
this.intField,
this.intFieldWithDefaultValue = 1,
});

final int requiredIntField;
final int? intField;
final int intFieldWithDefaultValue;

@override
Widget build(BuildContext context, GoRouterState state) => BasePage<int>(
dataTitle: 'IntRoute',
param: requiredIntField,
queryParam: intField,
queryParamWithDefaultValue: intFieldWithDefaultValue,
);

Widget drawerTile(BuildContext context) => ListTile(
Expand All @@ -156,16 +165,19 @@ class NumRoute extends GoRouteData {
NumRoute({
required this.requiredNumField,
this.numField,
this.numFieldWithDefaultValue = 1,
});

final num requiredNumField;
final num? numField;
final num numFieldWithDefaultValue;

@override
Widget build(BuildContext context, GoRouterState state) => BasePage<num>(
dataTitle: 'NumRoute',
param: requiredNumField,
queryParam: numField,
queryParamWithDefaultValue: numFieldWithDefaultValue,
);

Widget drawerTile(BuildContext context) => ListTile(
Expand All @@ -179,17 +191,20 @@ class EnumRoute extends GoRouteData {
EnumRoute({
required this.requiredEnumField,
this.enumField,
this.enumFieldWithDefaultValue = PersonDetails.favoriteFood,
});

final PersonDetails requiredEnumField;
final PersonDetails? enumField;
final PersonDetails enumFieldWithDefaultValue;

@override
Widget build(BuildContext context, GoRouterState state) =>
BasePage<PersonDetails>(
dataTitle: 'EnumRoute',
param: requiredEnumField,
queryParam: enumField,
queryParamWithDefaultValue: enumFieldWithDefaultValue,
);

Widget drawerTile(BuildContext context) => ListTile(
Expand All @@ -203,17 +218,20 @@ class EnhancedEnumRoute extends GoRouteData {
EnhancedEnumRoute({
required this.requiredEnumField,
this.enumField,
this.enumFieldWithDefaultValue = SportDetails.football,
});

final SportDetails requiredEnumField;
final SportDetails? enumField;
final SportDetails enumFieldWithDefaultValue;

@override
Widget build(BuildContext context, GoRouterState state) =>
BasePage<SportDetails>(
dataTitle: 'EnhancedEnumRoute',
param: requiredEnumField,
queryParam: enumField,
queryParamWithDefaultValue: enumFieldWithDefaultValue,
);

Widget drawerTile(BuildContext context) => ListTile(
Expand All @@ -227,16 +245,19 @@ class StringRoute extends GoRouteData {
StringRoute({
required this.requiredStringField,
this.stringField,
this.stringFieldWithDefaultValue = 'defaultValue',
});

final String requiredStringField;
final String? stringField;
final String stringFieldWithDefaultValue;

@override
Widget build(BuildContext context, GoRouterState state) => BasePage<String>(
dataTitle: 'StringRoute',
param: requiredStringField,
queryParam: stringField,
queryParamWithDefaultValue: stringFieldWithDefaultValue,
);

Widget drawerTile(BuildContext context) => ListTile(
Expand Down Expand Up @@ -274,12 +295,14 @@ class BasePage<T> extends StatelessWidget {
required this.dataTitle,
required this.param,
this.queryParam,
this.queryParamWithDefaultValue,
super.key,
});

final String dataTitle;
final T param;
final T? queryParam;
final T? queryParamWithDefaultValue;

@override
Widget build(BuildContext context) => Scaffold(
Expand Down Expand Up @@ -339,6 +362,9 @@ class BasePage<T> extends StatelessWidget {
Text(dataTitle),
Text('Param: $param'),
Text('Query param: $queryParam'),
Text(
'Query param with default value: $queryParamWithDefaultValue',
),
SelectableText(GoRouter.of(context).location),
],
),
Expand Down
47 changes: 47 additions & 0 deletions packages/go_router_builder/example/lib/all_types.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f2f6c5e

Please sign in to comment.