Skip to content

Commit 5690c37

Browse files
[go_router_builder] Add test for onExit (#6614)
Follows #6495 Fixes flutter/flutter#137394
1 parent 29101bf commit 5690c37

File tree

6 files changed

+206
-5
lines changed

6 files changed

+206
-5
lines changed

packages/go_router_builder/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## NEXT
1+
## 2.7.0
22

3-
* Updates minimum supported SDK version to Flutter 3.16/Dart 3.2.
3+
- Adds an example and a test with `onExit`.
4+
- Updates minimum supported SDK version to Flutter 3.16/Dart 3.2.
45

56
## 2.6.2
67

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// ignore_for_file: public_member_api_docs, unreachable_from_main
6+
7+
import 'package:flutter/material.dart';
8+
import 'package:go_router/go_router.dart';
9+
10+
part 'on_exit_example.g.dart';
11+
12+
void main() => runApp(App());
13+
14+
class App extends StatelessWidget {
15+
App({super.key});
16+
17+
@override
18+
Widget build(BuildContext context) => MaterialApp.router(
19+
routerConfig: _router,
20+
title: _appTitle,
21+
);
22+
23+
final GoRouter _router = GoRouter(routes: $appRoutes);
24+
}
25+
26+
@TypedGoRoute<HomeRoute>(
27+
path: '/',
28+
routes: <TypedGoRoute<GoRouteData>>[
29+
TypedGoRoute<SubRoute>(path: 'sub-route')
30+
],
31+
)
32+
class HomeRoute extends GoRouteData {
33+
const HomeRoute();
34+
35+
@override
36+
Widget build(BuildContext context, GoRouterState state) => const HomeScreen();
37+
}
38+
39+
class SubRoute extends GoRouteData {
40+
const SubRoute();
41+
42+
@override
43+
Future<bool> onExit(BuildContext context, GoRouterState state) async {
44+
final bool? confirmed = await showDialog<bool>(
45+
context: context,
46+
builder: (_) => AlertDialog(
47+
content: const Text('Are you sure to leave this page?'),
48+
actions: <Widget>[
49+
TextButton(
50+
onPressed: () => Navigator.of(context).pop(false),
51+
child: const Text('Cancel'),
52+
),
53+
ElevatedButton(
54+
onPressed: () => Navigator.of(context).pop(true),
55+
child: const Text('Confirm'),
56+
),
57+
],
58+
),
59+
);
60+
return confirmed ?? false;
61+
}
62+
63+
@override
64+
Widget build(BuildContext context, GoRouterState state) => const SubScreen();
65+
}
66+
67+
class HomeScreen extends StatelessWidget {
68+
const HomeScreen({super.key});
69+
70+
@override
71+
Widget build(BuildContext context) => Scaffold(
72+
appBar: AppBar(title: const Text(_appTitle)),
73+
body: Center(
74+
child: ElevatedButton(
75+
onPressed: () => const SubRoute().go(context),
76+
child: const Text('Go to sub screen'),
77+
),
78+
));
79+
}
80+
81+
class SubScreen extends StatelessWidget {
82+
const SubScreen({super.key});
83+
84+
@override
85+
Widget build(BuildContext context) => Scaffold(
86+
appBar: AppBar(title: const Text('$_appTitle Sub screen')),
87+
body: Center(
88+
child: ElevatedButton(
89+
onPressed: () => Navigator.of(context).pop(),
90+
child: const Text('Go back'),
91+
),
92+
),
93+
);
94+
}
95+
96+
const String _appTitle = 'GoRouter Example: builder';

packages/go_router_builder/example/lib/on_exit_example.g.dart

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/go_router_builder/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies:
99
collection: ^1.15.0
1010
flutter:
1111
sdk: flutter
12-
go_router: ^10.0.0
12+
go_router: ^14.1.1
1313
provider: 6.0.5
1414

1515
dev_dependencies:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:go_router_builder_example/on_exit_example.dart';
8+
9+
void main() {
10+
testWidgets('It should trigger the on exit when leaving the route',
11+
(WidgetTester tester) async {
12+
await tester.pumpWidget(App());
13+
expect(find.byType(HomeScreen), findsOne);
14+
15+
await tester.tap(find.widgetWithText(ElevatedButton, 'Go to sub screen'));
16+
await tester.pumpAndSettle();
17+
18+
expect(find.byType(SubScreen), findsOne);
19+
20+
await tester.tap(find.widgetWithText(ElevatedButton, 'Go back'));
21+
await tester.pumpAndSettle();
22+
23+
expect(
24+
find.widgetWithText(AlertDialog, 'Are you sure to leave this page?'),
25+
findsOne,
26+
);
27+
await tester.tap(find.text('Cancel'));
28+
await tester.pumpAndSettle();
29+
30+
expect(find.byType(HomeScreen), findsNothing);
31+
expect(find.byType(SubScreen), findsOne);
32+
33+
await tester.tap(find.widgetWithText(ElevatedButton, 'Go back'));
34+
await tester.pumpAndSettle();
35+
36+
expect(
37+
find.widgetWithText(AlertDialog, 'Are you sure to leave this page?'),
38+
findsOne,
39+
);
40+
await tester.tap(find.text('Confirm'));
41+
await tester.pumpAndSettle();
42+
43+
expect(find.byType(HomeScreen), findsOne);
44+
expect(find.byType(SubScreen), findsNothing);
45+
});
46+
}

packages/go_router_builder/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: go_router_builder
22
description: >-
33
A builder that supports generated strongly-typed route helpers for
44
package:go_router
5-
version: 2.6.2
5+
version: 2.7.0
66
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
77
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22
88

@@ -26,7 +26,7 @@ dev_dependencies:
2626
dart_style: 2.3.6
2727
flutter:
2828
sdk: flutter
29-
go_router: ^10.0.0
29+
go_router: ^14.0.0
3030
test: ^1.20.0
3131

3232
topics:

0 commit comments

Comments
 (0)