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_router_builder] Fixed the return value of the generated push method #3650

Merged
merged 12 commits into from
Apr 21, 2023
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.2.2

* Supports returning value in generated `push` method. [go_router CHANGELOG](https://github.com/flutter/packages/blob/main/packages/go_router/CHANGELOG.md#650)

## 1.2.1

* Supports opt-in required extra parameters. [#117261](https://github.com/flutter/flutter/issues/117261)
Expand Down
11 changes: 11 additions & 0 deletions packages/go_router_builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ void _tap() => PersonRoute(pid: 'p1').go(context);

This is the point of typed routing: the error is found statically.

## Return value

Starting from `go_router` 6.5.0, pushing a route and subsequently popping it, can produce
a return value. The generated routes also follow this functionality.

```dart
void _tap() async {
final result = await PersonRoute(pid: 'p1').go(context);
}
```

## Query parameters

Optional parameters (named or positional) indicate query parameters:
Expand Down
26 changes: 13 additions & 13 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.

8 changes: 5 additions & 3 deletions packages/go_router_builder/example/lib/extra_example.g.dart

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

83 changes: 74 additions & 9 deletions packages/go_router_builder/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class App extends StatelessWidget {
],
),
],
)
),
TypedGoRoute<FamilyCountRoute>(path: 'family-count/:count'),
],
)
class HomeRoute extends GoRouteData {
Expand Down Expand Up @@ -149,6 +150,17 @@ class PersonDetailsRoute extends GoRouteData {
}
}

class FamilyCountRoute extends GoRouteData {
const FamilyCountRoute(this.count);

final int count;

@override
Widget build(BuildContext context, GoRouterState state) => FamilyCountScreen(
count: count,
);
}

class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});

Expand All @@ -161,14 +173,38 @@ class HomeScreen extends StatelessWidget {
title: const Text(App.title),
centerTitle: true,
actions: <Widget>[
ElevatedButton(
onPressed: () => const PersonRoute('f1', 1).push(context),
child: const Text('Push a route'),
),
IconButton(
onPressed: info.logout,
tooltip: 'Logout: ${info.userName}',
icon: const Icon(Icons.logout),
PopupMenuButton<String>(
itemBuilder: (BuildContext context) {
return <PopupMenuItem<String>>[
PopupMenuItem<String>(
value: '1',
child: const Text('Push w/o return value'),
onTap: () => const PersonRoute('f1', 1).push(context),
),
PopupMenuItem<String>(
value: '2',
child: const Text('Push w/ return value'),
onTap: () async {
FamilyCountRoute(familyData.length)
.push<int>(context)
.then((int? value) {
if (value != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Age was: $value'),
),
);
}
});
},
),
PopupMenuItem<String>(
value: '3',
child: Text('Logout: ${info.userName}'),
onTap: () => info.logout(),
),
];
},
),
],
),
Expand Down Expand Up @@ -277,6 +313,35 @@ class PersonDetailsPage extends StatelessWidget {
);
}

class FamilyCountScreen extends StatelessWidget {
const FamilyCountScreen({super.key, required this.count});

final int count;

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Family Count')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Center(
child: Text(
'There are $count families',
style: Theme.of(context).textTheme.headlineSmall,
),
),
ElevatedButton(
onPressed: () => context.pop(count),
child: Text('Pop with return value $count'),
),
],
),
),
);
}

class LoginScreen extends StatelessWidget {
const LoginScreen({this.from, super.key});
final String? from;
Expand Down
32 changes: 27 additions & 5 deletions packages/go_router_builder/example/lib/main.g.dart

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

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

Loading