搜尋套件、儲存庫與 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.

所屬分類

原始碼儲存庫

使用此套件的專案