Coder Social home page Coder Social logo

dragun98 / flutter_window_close Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zonble/flutter_window_close

0.0 0.0 0.0 85.85 MB

Helps your Flutter apps to handle window close events.

Home Page: https://pub.dev/packages/flutter_window_close

License: MIT License

Ruby 3.00% C++ 43.65% C 3.87% Dart 16.26% Swift 3.38% HTML 5.43% CMake 24.42%

flutter_window_close's Introduction

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.

flutter_window_close's People

Contributors

zonble avatar doppio avatar alexmercerind avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.