v2.1.1
flutter_exit_app
一个 Flutter 插件,提供一种平台特定的方法来退出应用,而无需在 Dart 代码中调用 exit(0)。
61喜欢 2万近 30 天下载160Pub 分数
AndroidiOS
一个用于退出应用的 Flutter 插件
{"sdk":"flutter"}{"sdk":"flutter"}^5.0.0以下为英文项目原文快照,最新内容请访问 GitHub。
A Flutter plugin that provides a platform-specific way to exit your application without calling exit(0) in Dart code.
| Platform | Minimum Version |
|---|---|
| Android | API 21 (Android 5.0) |
| iOS | 12.0 |
Add this to your package's pubspec.yaml file:
dependencies:
flutter_exit_app: ^2.1.1
Then run:
flutter pub get
import 'package:flutter_exit_app/flutter_exit_app.dart';
// Exit the app using platform-specific methods
// iOS: Suspends the app (Apple guideline compliant)
// Android: Finishes activity and exits
await FlutterExitApp.exitApp();
// Force-kill the iOS app process (against Apple guidelines)
// Only use when absolutely necessary
await FlutterExitApp.exitApp(iosForceExit: true);
import 'package:flutter/material.dart';
import 'package:flutter_exit_app/flutter_exit_app.dart';
class ExitButton extends StatelessWidget {
const ExitButton({super.key});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () async {
// Show confirmation dialog
final shouldExit = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Exit App'),
content: const Text('Are you sure you want to exit?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Exit'),
),
],
),
);
if (shouldExit == true) {
await FlutterExitApp.exitApp();
}
},
child: const Text('Exit App'),
);
}
}
Apple's Human Interface Guidelines discourage apps from programmatically exiting. The plugin provides two modes:
Default mode (iosForceExit: false):
Force exit mode (iosForceExit: true):
On Android, the plugin finishes the current activity and removes it from the recent tasks list, then exits the process after a 1-second delay to ensure clean shutdown. The app is completely removed from the task manager.
See the LICENSE file for details.