skip to Main Content

enter image description hereI have a flutter app that i published on play store. I download it on my acer chromebook and i see the window bar. I want to show a message when the user want to close the app by clicking on the X of the window bar. How can i do this in flutter?

2

Answers


  1. You can use the WillPopScope widget to display a confirmation message before the app exits. This widget triggers whenever the user attempts to navigate away from the current screen, which can include closing the app.

    Here’s an example:

    Widget build(BuildContext context) {
      return WillPopScope(
        onWillPop: () async {
          final shouldExit = await showDialog(
            context: context,
            builder: (context) => AlertDialog(
              title: Text('Are you sure you want to exit?'),
              content: Text('Any unsaved changes will be lost.'),
              actions: [
                TextButton(
                  onPressed: () => Navigator.pop(context, false),
                  child: Text('Cancel'),
                ),
                TextButton(
                  onPressed: () => Navigator.pop(context, true),
                  child: Text('Exit'),
                ),
              ],
            ),
          );
          return shouldExit ?? false;
        },
        child: MyHomePage(), // Your app's main content
      );
    }
    
    Login or Signup to reply.
  2. You can do it using the existing flutter_window_close package. Check out the example here.

    This gives you the possibility to pop a confirmation to the user, and it works on Linux, windows, MacOS.

    The is also this bitsdojo_window which gives you the possibility to customize your windows, manage confirmation dialog on app close button press and more…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search