Add support for application exit requests (#121378)
Add support for application exit requests
This commit is contained in:
110
examples/api/lib/services/binding/handle_request_app_exit.0.dart
Normal file
110
examples/api/lib/services/binding/handle_request_app_exit.0.dart
Normal file
@@ -0,0 +1,110 @@
|
||||
// Copyright 2014 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 'dart:ui';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// Flutter code sample for [ServicesBinding.handleRequestAppExit].
|
||||
|
||||
void main() {
|
||||
runApp(const ApplicationExitExample());
|
||||
}
|
||||
|
||||
class ApplicationExitExample extends StatelessWidget {
|
||||
const ApplicationExitExample({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: Scaffold(body: Body()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Body extends StatefulWidget {
|
||||
const Body({super.key});
|
||||
|
||||
@override
|
||||
State<Body> createState() => _BodyState();
|
||||
}
|
||||
|
||||
class _BodyState extends State<Body> with WidgetsBindingObserver {
|
||||
bool _shouldExit = false;
|
||||
String lastResponse = 'No exit requested yet';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _quit() async {
|
||||
final AppExitType exitType = _shouldExit ? AppExitType.required : AppExitType.cancelable;
|
||||
setState(() {
|
||||
lastResponse = 'App requesting ${exitType.name} exit';
|
||||
});
|
||||
await ServicesBinding.instance.exitApplication(exitType);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AppExitResponse> didRequestAppExit() async {
|
||||
final AppExitResponse response = _shouldExit ? AppExitResponse.exit : AppExitResponse.cancel;
|
||||
setState(() {
|
||||
lastResponse = 'App responded ${response.name} to exit request';
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
void _radioChanged(bool? value) {
|
||||
value ??= true;
|
||||
if (_shouldExit == value) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_shouldExit = value!;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: SizedBox(
|
||||
width: 300,
|
||||
child: IntrinsicHeight(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
RadioListTile<bool>(
|
||||
title: const Text('Do Not Allow Exit'),
|
||||
groupValue: _shouldExit,
|
||||
value: false,
|
||||
onChanged: _radioChanged,
|
||||
),
|
||||
RadioListTile<bool>(
|
||||
title: const Text('Allow Exit'),
|
||||
groupValue: _shouldExit,
|
||||
value: true,
|
||||
onChanged: _radioChanged,
|
||||
),
|
||||
const SizedBox(height: 30),
|
||||
ElevatedButton(
|
||||
onPressed: _quit,
|
||||
child: const Text('Quit'),
|
||||
),
|
||||
const SizedBox(height: 30),
|
||||
Text(lastResponse),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,6 @@
|
||||
<key>NSMainNibFile</key>
|
||||
<string>MainMenu</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
<string>FlutterApplication</string>
|
||||
<string>NSApplication</string>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2014 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_api_samples/services/binding/handle_request_app_exit.0.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Application Exit example', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.ApplicationExitExample(),
|
||||
);
|
||||
|
||||
expect(find.text('No exit requested yet'), findsOneWidget);
|
||||
expect(find.text('Do Not Allow Exit'), findsOneWidget);
|
||||
expect(find.text('Allow Exit'), findsOneWidget);
|
||||
expect(find.text('Quit'), findsOneWidget);
|
||||
await tester.tap(find.text('Quit'));
|
||||
await tester.pump();
|
||||
expect(find.text('App requesting cancelable exit'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user