搜尋套件、儲存庫與 APP 分類

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 項

原始碼儲存庫

使用此套件的專案