搜索插件、代码仓与 APP 分类

插件详情v2.1.2

flutter_exit_app

一个 Flutter 插件,提供一种平台特定的方法来退出应用,而无需在 Dart 代码中调用 exit(0)。

AndroidiOS
近 30 天下载
2.3万
喜欢数
62
Pub 分数
160

安装插件

flutter pub add flutter_exit_app

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 插件,可实现平台特定的应用程序终止,允许用户在不使用 Dart 的 exit(0) 的情况下优雅地退出应用程序。它利用原生平台方法,确保在 Android 和 iOS 上的兼容性和正确行为。该插件专为希望以干净方式响应用户操作(如按下返回按钮或从菜单中选择“退出”)来关闭应用程序的开发者设计。它避免直接终止进程,从而促进更好的应用程序生命周期管理。

适合场景

  • 返回键退出应用
  • 实现退出确认对话框
  • 从设置菜单关闭应用
  • 在自助模式下优雅关闭
  • 全屏应用中的退出处理

主要特点

  • 平台特定的退出行为
  • 无需调用 exit(0)
  • 支持 Android 和 iOS
  • 简单的 API 集成
  • 尊重应用生命周期

适合对象

  • 开发用于自助服务终端或全屏应用的开发者
  • 应用在退出前需要用户确认
  • 需要原生退出行为的项目
  • 具有自定义导航流程的移动应用
  • 针对 Android 和 iOS 的应用

使用建议

  • 在某些平台上,调试模式下无法正常工作
  • 可能需要额外处理后台任务
  • 不适用于依赖 exit(0) 进行测试的应用程序
  • 对退出时机的控制有限
  • Android 上需要正确处理权限

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_exit_app

A Flutter plugin that provides a platform-specific way to exit your application without calling exit(0) in Dart code.

Features

  • Android: Cleanly finishes the activity and removes it from the task list
  • iOS: Suspends the application (Apple guideline compliant) with optional force-exit
  • Modern implementation: Written in Kotlin (Android) and Swift (iOS)
  • Type-safe: Full null-safety support
  • Well-tested: Comprehensive unit tests

Platform Support

Platform Minimum Version
Android API 21 (Android 5.0)
iOS 12.0

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  flutter_exit_app: ^2.1.1

Then run:

flutter pub get

Usage

Import the package
import 'package:flutter_exit_app/flutter_exit_app.dart';
Basic exit (recommended)
// Exit the app using platform-specific methods
// iOS: Suspends the app (Apple guideline compliant)
// Android: Finishes activity and exits
await FlutterExitApp.exitApp();
Force exit on iOS (not recommended)
// Force-kill the iOS app process (against Apple guidelines)
// Only use when absolutely necessary
await FlutterExitApp.exitApp(iosForceExit: true);
Complete example
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'),
    );
  }
}

Important Notes

iOS Guidelines

Apple's Human Interface Guidelines discourage apps from programmatically exiting. The plugin provides two modes:

Default mode (iosForceExit: false):

  • Animates the app to background with a smooth fade-out effect
  • Suspends the app gracefully
  • App remains in memory until iOS decides to terminate it
  • Recommended: This is the Apple-approved approach

Force exit mode (iosForceExit: true):

  • Animates the app with fade-out and scale-down effect
  • Forcibly terminates the process after animation
  • Warning: The app will appear to "crash" to users
  • May be rejected during App Store review
  • Only use if you have a specific requirement (e.g., enterprise apps)
Android Behavior

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.

License

See the LICENSE file for details.

所属分类

源码仓库

使用此插件的项目