首页 > 解决方案 > 按下appbar后退按钮时如何确认应用退出

问题描述

当按下应用栏上的后退按钮时,我想确认退出应用。

 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但在我的情况下它们都不起作用。

帮帮我

标签: flutterdart

解决方案


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

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


推荐阅读