v5
This commit is contained in:
139
lib/go_router/examples/on_exit.dart
Normal file
139
lib/go_router/examples/on_exit.dart
Normal file
@@ -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'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user