如何在用户关闭 Flutter 中的应用程序之前显示消息?

问题描述 投票:0回答:2

我有一个在 Play 商店上发布的 flutter 应用程序。我在我的 acer chromebook 上下载了它,然后看到了窗口栏。我想在用户想要通过单击窗口栏的 X 来关闭应用程序时显示一条消息。我怎样才能在颤振中做到这一点?

android flutter chromebook
2个回答
0
投票

您可以使用 WillPopScope 小部件在应用程序退出之前显示确认消息。每当用户尝试离开当前屏幕(其中可能包括关闭应用程序)时,此小部件就会触发。

这是一个例子:

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
  );
}

0
投票

您可以使用现有的 flutter_window_close 包来完成此操作。查看示例此处

这使您可以向用户弹出确认信息,并且它适用于 Linux、Windows、MacOS。

这也是bitsdojo_window,它使您可以自定义窗口、管理按下应用程序关闭按钮时的确认对话框等等...

© www.soinside.com 2019 - 2024. All rights reserved.