diff --git a/dev/benchmarks/complex_layout/macos/Runner/Info.plist b/dev/benchmarks/complex_layout/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/dev/benchmarks/complex_layout/macos/Runner/Info.plist
+++ b/dev/benchmarks/complex_layout/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/dev/benchmarks/macrobenchmarks/macos/Runner/Info.plist b/dev/benchmarks/macrobenchmarks/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/dev/benchmarks/macrobenchmarks/macos/Runner/Info.plist
+++ b/dev/benchmarks/macrobenchmarks/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/dev/integration_tests/channels/macos/Runner/Info.plist b/dev/integration_tests/channels/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/dev/integration_tests/channels/macos/Runner/Info.plist
+++ b/dev/integration_tests/channels/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/dev/integration_tests/flavors/macos/Runner/Info.plist b/dev/integration_tests/flavors/macos/Runner/Info.plist
index eba8f244a5..e6e728b552 100644
--- a/dev/integration_tests/flavors/macos/Runner/Info.plist
+++ b/dev/integration_tests/flavors/macos/Runner/Info.plist
@@ -29,6 +29,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/dev/integration_tests/flutter_gallery/macos/Runner/Info.plist b/dev/integration_tests/flutter_gallery/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/dev/integration_tests/flutter_gallery/macos/Runner/Info.plist
+++ b/dev/integration_tests/flutter_gallery/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/dev/integration_tests/ui/macos/Runner/Info.plist b/dev/integration_tests/ui/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/dev/integration_tests/ui/macos/Runner/Info.plist
+++ b/dev/integration_tests/ui/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/dev/manual_tests/macos/Runner/Info.plist b/dev/manual_tests/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/dev/manual_tests/macos/Runner/Info.plist
+++ b/dev/manual_tests/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/examples/api/lib/services/binding/handle_request_app_exit.0.dart b/examples/api/lib/services/binding/handle_request_app_exit.0.dart
new file mode 100644
index 0000000000..3578f435fb
--- /dev/null
+++ b/examples/api/lib/services/binding/handle_request_app_exit.0.dart
@@ -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
createState() => _BodyState();
+}
+
+class _BodyState extends State 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 _quit() async {
+ final AppExitType exitType = _shouldExit ? AppExitType.required : AppExitType.cancelable;
+ setState(() {
+ lastResponse = 'App requesting ${exitType.name} exit';
+ });
+ await ServicesBinding.instance.exitApplication(exitType);
+ }
+
+ @override
+ Future 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: [
+ RadioListTile(
+ title: const Text('Do Not Allow Exit'),
+ groupValue: _shouldExit,
+ value: false,
+ onChanged: _radioChanged,
+ ),
+ RadioListTile(
+ 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),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/examples/api/macos/Runner/Info.plist b/examples/api/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/examples/api/macos/Runner/Info.plist
+++ b/examples/api/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/examples/api/test/services/binding/handle_request_app_exit.0_test.dart b/examples/api/test/services/binding/handle_request_app_exit.0_test.dart
new file mode 100644
index 0000000000..8f153ae9cc
--- /dev/null
+++ b/examples/api/test/services/binding/handle_request_app_exit.0_test.dart
@@ -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);
+ });
+}
diff --git a/examples/flutter_view/macos/Runner/Info.plist b/examples/flutter_view/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/examples/flutter_view/macos/Runner/Info.plist
+++ b/examples/flutter_view/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/examples/hello_world/macos/Runner/Info.plist b/examples/hello_world/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/examples/hello_world/macos/Runner/Info.plist
+++ b/examples/hello_world/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/examples/image_list/macos/Runner/Info.plist b/examples/image_list/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/examples/image_list/macos/Runner/Info.plist
+++ b/examples/image_list/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/examples/layers/macos/Runner/Info.plist b/examples/layers/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/examples/layers/macos/Runner/Info.plist
+++ b/examples/layers/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/examples/platform_view/macos/Runner/Info.plist b/examples/platform_view/macos/Runner/Info.plist
index 55c6534ef3..4789daa6a4 100644
--- a/examples/platform_view/macos/Runner/Info.plist
+++ b/examples/platform_view/macos/Runner/Info.plist
@@ -27,6 +27,6 @@
NSMainNibFile
MainMenu
NSPrincipalClass
- FlutterApplication
+ NSApplication
diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart
index b9531f419c..16441b2763 100644
--- a/packages/flutter/lib/src/services/binding.dart
+++ b/packages/flutter/lib/src/services/binding.dart
@@ -263,30 +263,119 @@ mixin ServicesBinding on BindingBase, SchedulerBinding {
return null;
}
- Future _handlePlatformMessage(MethodCall methodCall) async {
+ Future _handlePlatformMessage(MethodCall methodCall) async {
final String method = methodCall.method;
- // There is only one incoming method call currently possible.
- assert(method == 'SystemChrome.systemUIChange');
- final List args = methodCall.arguments as List;
- if (_systemUiChangeCallback != null) {
- await _systemUiChangeCallback!(args[0] as bool);
+ assert(method == 'SystemChrome.systemUIChange' || method == 'System.requestAppExit');
+ switch (method) {
+ case 'SystemChrome.systemUIChange':
+ final List args = methodCall.arguments as List;
+ if (_systemUiChangeCallback != null) {
+ await _systemUiChangeCallback!(args[0] as bool);
+ }
+ break;
+ case 'System.requestAppExit':
+ return {'response': (await handleRequestAppExit()).name};
}
}
static AppLifecycleState? _parseAppLifecycleMessage(String message) {
switch (message) {
- case 'AppLifecycleState.paused':
- return AppLifecycleState.paused;
case 'AppLifecycleState.resumed':
return AppLifecycleState.resumed;
case 'AppLifecycleState.inactive':
return AppLifecycleState.inactive;
+ case 'AppLifecycleState.paused':
+ return AppLifecycleState.paused;
case 'AppLifecycleState.detached':
return AppLifecycleState.detached;
}
return null;
}
+ /// Handles any requests for application exit that may be received on the
+ /// [SystemChannels.platform] method channel.
+ ///
+ /// By default, returns [ui.AppExitResponse.exit].
+ ///
+ /// {@template flutter.services.binding.ServicesBinding.requestAppExit}
+ /// Not all exits are cancelable, so not all exits will call this function. Do
+ /// not rely on this function as a place to save critical data, because you
+ /// will be disappointed. There are a number of ways that the application can
+ /// exit without letting the application know first: power can be unplugged,
+ /// the battery removed, the application can be killed in a task manager or
+ /// command line, or the device could have a rapid unplanned disassembly (i.e.
+ /// it could explode). In all of those cases (and probably others), no
+ /// notification will be given to the application that it is about to exit.
+ /// {@endtemplate}
+ ///
+ /// {@tool sample}
+ /// This examples shows how an application can cancel (or not) OS requests for
+ /// quitting an application. Currently this is only supported on macOS and
+ /// Linux.
+ ///
+ /// ** See code in examples/api/lib/services/binding/handle_request_app_exit.0.dart **
+ /// {@end-tool}
+ ///
+ /// See also:
+ ///
+ /// * [WidgetsBindingObserver.didRequestAppExit], which can be overridden to
+ /// respond to this message.
+ /// * [WidgetsBinding.handleRequestAppExit] which overrides this method to
+ /// notify its observers.
+ Future handleRequestAppExit() async {
+ return ui.AppExitResponse.exit;
+ }
+
+ /// Exits the application by calling the native application API method for
+ /// exiting an application cleanly.
+ ///
+ /// This differs from calling `dart:io`'s [exit] function in that it gives the
+ /// engine a chance to clean up resources so that it doesn't crash on exit, so
+ /// calling this is always preferred over calling [exit]. It also optionally
+ /// gives handlers of [handleRequestAppExit] a chance to cancel the
+ /// application exit.
+ ///
+ /// The [exitType] indicates what kind of exit to perform. For
+ /// [ui.AppExitType.cancelable] exits, the application is queried through a
+ /// call to [handleRequestAppExit], where the application can optionally
+ /// cancel the request for exit. If the [exitType] is
+ /// [ui.AppExitType.required], then the application exits immediately without
+ /// querying the application.
+ ///
+ /// For [ui.AppExitType.cancelable] exits, the returned response value is the
+ /// response obtained from the application as to whether the exit was canceled
+ /// or not. Practically, the response will never be [ui.AppExitResponse.exit],
+ /// since the application will have already exited by the time the result
+ /// would have been received.
+ ///
+ /// The optional [exitCode] argument will be used as the application exit code
+ /// on platforms where an exit code is supported. On other platforms it may be
+ /// ignored. It defaults to zero.
+ ///
+ /// See also:
+ ///
+ /// * [WidgetsBindingObserver.didRequestAppExit] for a handler you can
+ /// override on a [WidgetsBindingObserver] to receive exit requests.
+ @mustCallSuper
+ Future exitApplication(ui.AppExitType exitType, [int exitCode = 0]) async {
+ final Map? result = await SystemChannels.platform.invokeMethod