パッケージ、リポジトリ、アプリカテゴリを検索

flutter_window_close のカバー
パッケージ詳細v1.3.0

flutter_window_close

Flutterアプリが、ユーザーが本当にアプリを終了したいかどうかを確認できるようにします。

WebmacOSWindowsLinux
30日間ダウンロード
3万
いいね数
99
Pub ポイント
160

パッケージをインストール

flutter pub add flutter_window_close

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

flutter_window_close パッケージは、デスクトッププラットフォーム上の Flutter アプリケーションがウィンドウの閉じるイベントをインターセプトし、終了前にユーザーに確認を求める機能を提供します。未保存のデータを保存する必要があるアプリや、誤って閉じるのを防ぎたいアプリにとって特に役立ちます。ユーザーがウィンドウを閉じようとするときに実行されるコールバックを登録するための簡単な API を提供しており、開発者はカスタムダイアログを表示したり、クリーンアップタスクを実行したりできます。このパッケージは macOS、Windows、Linux、Web をサポートしており、グレースフルシャットダウン処理が必要なクロスプラットフォームデスクトップアプリに最適です。

用途

  • 誤ってアプリを閉じるのを防ぐ
  • 終了前に保存されていないデータを保存する
  • 確認ダイアログの表示
  • デスクトップでのスムーズなシャットダウン
  • カスタム終了ロジック
  • Web およびデスクトップ互換性

主な機能

  • ウィンドウの閉じるイベントをインターセプトします
  • 複数のデスクトッププラットフォームをサポート
  • シンプルなコールバック API
  • クロスプラットフォームデスクトップサポート
  • オプションの確認ダイアログ
  • Web およびネイティブ統合

適している対象

  • デスクトップ Flutter アプリ
  • 保存されていないデータのあるアプリ
  • ユーザー向けデスクトップアプリケーション
  • エグジット制御が必要な開発者
  • 確認プロンプトを必要とするアプリケーション
  • クロスプラットフォームデスクトッププロジェクト

注意事項

  • モバイルプラットフォームでは適用されません
  • プラットフォーム固有の設定が必要です
  • システムレベルの閉じる動作と競合する可能性があります
  • デスクトップおよびウェブに限定
  • タスクマネージャーからのアプリ終了を処理しない
  • ユーザー体験は実装に依存します

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

flutter_window_close

2021 © Weizhong Yang a.k.a zonble

Pub example workflow License: MIT

flutter_window_close lets your Flutter app has a chance to confirm if the user wants to close your app. It works on desktop platforms including Windows, macOS and Linux.

macOS Windows Linux

Example

Getting Started

It is very common that an app would prompt a message like "Do you really want to quit" when users click on the close button, in order to notify that there are still undone tasks and the users may lose their data if they want to quit anyway. It prevents the users from losing data unwillingly.

To let a Flutter desktop app to support that, the plug-in listens to the events from the window hosting Flutter's view, and send the events to Flutter. What you need to do is to assign an anonymous function that can answer if the window should be closed. For example, you can show an alert dialog to ask what the current user is willing to do:

FlutterWindowClose.setWindowShouldCloseHandler(() async {
    return await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
          title: const Text('Do you really want to quit?'),
          actions: [
            ElevatedButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: const Text('Yes')),
            ElevatedButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: const Text('No')),
          ]);
        });
});

The plugin bridges following APIs:

It does not support mobile platforms, since there is no such event for closing windows. You can use a custom WillPopScope to capture if a user is leaving your app with a back key.

macOS

There could be some issues while using the package on macOS. Each platform has its paradigm and the developer framework macOS sees window objects in a different way from Windows and Linux.

On Windows and Linux, windows are more like controllers in MVC pattern , and when it comes to Flutter, there would be always a root window in the process of an app, and our plugin could easily know which is the window to listen to. In the code level, we use GetActiveWindow while we can use gtk_widget_get_ancestor or gtk_widget_get_toplevel.

On the contrary, windows are more like views on macOS. An app can have multiple windows, and the app can stay still open event all windows are closed. We can also create an object with multiple IBOutlets to multiple windows. Flutter macOS does not tell plugins which window is the one running Flutter as well.

The plugin listens to the first window in the windows list of the singleton NSApplication object. It works if you have only one window in your macOS Flutter app. If you just create a new app using the official template for macOS, you may need not to change anything. However, if your app has multiple windows, the behavior of the plugin might be unexpectable.

Flutter Web

We can do little when a user is closing the tab or window hosting your Flutter Web app. The only way to let a user to confirm if he or she really want to close is to set the return value of the onbeforeunload event. You can use the setWebReturnValue method to set the return value.

License

The package is released under MIT license.

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト