-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
related flutter/flutter#102408
- Loading branch information
Showing
8 changed files
with
511 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// 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'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
/// This sample app demonstrates how to use GoRoute.onExit. | ||
void main() => runApp(const MyApp()); | ||
|
||
/// The route configuration. | ||
final GoRouter _router = GoRouter( | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: '/', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const HomeScreen(); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const DetailsScreen(); | ||
}, | ||
onExit: (BuildContext context) async { | ||
final bool? confirmed = await showDialog<bool>( | ||
context: context, | ||
builder: (_) { | ||
return AlertDialog( | ||
content: const Text('Are you sure to leave this page?'), | ||
actions: <Widget>[ | ||
TextButton( | ||
onPressed: () => Navigator.of(context).pop(false), | ||
child: const Text('Cancel'), | ||
), | ||
TextButton( | ||
onPressed: () => Navigator.of(context).pop(true), | ||
child: const Text('Confirm'), | ||
), | ||
], | ||
); | ||
}, | ||
); | ||
return confirmed ?? false; | ||
}, | ||
), | ||
GoRoute( | ||
path: 'settings', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const SettingsScreen(); | ||
}, | ||
), | ||
], | ||
), | ||
], | ||
); | ||
|
||
/// The main app. | ||
class MyApp extends StatelessWidget { | ||
/// Constructs a [MyApp] | ||
const MyApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp.router( | ||
routerConfig: _router, | ||
); | ||
} | ||
} | ||
|
||
/// The home screen | ||
class HomeScreen extends StatelessWidget { | ||
/// Constructs a [HomeScreen] | ||
const HomeScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Home Screen')), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
ElevatedButton( | ||
onPressed: () => context.go('/details'), | ||
child: const Text('Go to the Details screen'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// The details screen | ||
class DetailsScreen extends StatelessWidget { | ||
/// Constructs a [DetailsScreen] | ||
const DetailsScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Details Screen')), | ||
body: Center( | ||
child: Column( | ||
children: <Widget>[ | ||
TextButton( | ||
onPressed: () { | ||
context.pop(); | ||
}, | ||
child: const Text('go back'), | ||
), | ||
TextButton( | ||
onPressed: () { | ||
context.go('/settings'); | ||
}, | ||
child: const Text('go to settings'), | ||
), | ||
], | ||
)), | ||
); | ||
} | ||
} | ||
|
||
/// The settings screen | ||
class SettingsScreen extends StatelessWidget { | ||
/// Constructs a [SettingsScreen] | ||
const SettingsScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Settings Screen')), | ||
body: const Center( | ||
child: Text('Settings'), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// 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'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router_examples/on_exit.dart' as example; | ||
|
||
void main() { | ||
testWidgets('example works', (WidgetTester tester) async { | ||
await tester.pumpWidget(const example.MyApp()); | ||
|
||
await tester.tap(find.text('Go to the Details screen')); | ||
await tester.pumpAndSettle(); | ||
|
||
await tester.tap(find.byType(BackButton)); | ||
await tester.pumpAndSettle(); | ||
|
||
expect(find.text('Are you sure to leave this page?'), findsOneWidget); | ||
await tester.tap(find.text('Cancel')); | ||
await tester.pumpAndSettle(); | ||
expect(find.byType(example.DetailsScreen), findsOneWidget); | ||
|
||
await tester.tap(find.byType(BackButton)); | ||
await tester.pumpAndSettle(); | ||
|
||
expect(find.text('Are you sure to leave this page?'), findsOneWidget); | ||
await tester.tap(find.text('Confirm')); | ||
await tester.pumpAndSettle(); | ||
expect(find.byType(example.HomeScreen), findsOneWidget); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.