【问题标题】:How to confirm app exit when appbar back button is pressed按下appbar后退按钮时如何确认应用退出
【发布时间】:2021-04-16 18:41:48
【问题描述】:

我想在按下应用栏上的返回按钮时确认退出应用。

 appBar: AppBar(
          leading: IconButton(
            icon: Icon(
              Icons.arrow_back_ios,
              color: Colors.white,
            ),
            onPressed: () {
              showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    title: Text('Save and Exit?'),
                    content: Text('are you sure you want to save and exit?'),
                    actions: [
                      FlatButton(
                        onPressed: () => Navigator.pop(context, false),
                        child: Text('No'),
                      ),
                      FlatButton(
                        onPressed: () => Navigator.pop(context, true),
                        child: Text('Yes'),
                      ),
                    ],
                  );
                },
              );
              // Navigator.pop(context);
            },
          ),

我试过了,但没有用。 当使用WillPopScope 按下系统后退按钮时,我找到了一些关于如何执行此操作的答案,但在我的情况下它们都不起作用。

帮帮我

【问题讨论】:

  • Navigator.pop 应该被调用两次,一次用于关闭对话框,另一次用于关闭应用程序。只需在“是”中添加另一个Navigator.pop

标签: flutter dart


【解决方案1】:

我猜你可以用 Navigator.canPop(context) 检查它。它将返回真或假。 在 onPressed 你可以检查它,如果它是真的你可以做 Navigator.pop(context) 否则调用 showDialog.

有文档的链接 https://api.flutter.dev/flutter/widgets/Navigator/canPop.html

【讨论】:

    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2019-06-17
    • 2018-02-08
    • 2021-11-21
    • 2019-08-28
    • 1970-01-01
    相关资源
    最近更新 更多