按下应用程序后退按钮时如何确认应用程序退出 [英] How to confirm app exit when appbar back button is pressed

查看:59
本文介绍了按下应用程序后退按钮时如何确认应用程序退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下应用栏上的后退按钮时确认退出该应用.

I want to confirm exiting the app when the back button on the appbar is pressed.

 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 按下系统后退按钮时如何执行操作的答案,但是在我的情况下,这些方法都不起作用.

I tried this but didn't work. I have found some answers on how to do it when the system back button is pressed using WillPopScope but none of them work in my case.

帮帮我

推荐答案

您可以使用Navigator.canPop(context)进行检查.它将返回true或false.在onPressed中可以检查它,如果为true,则可以执行Navigator.pop(context),否则调用showDialog.

You can check it with Navigator.canPop(context) i guess. It will return true or false. in onPressed you can check it, if it's true you can do Navigator.pop(context) otherwise call showDialog.

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

这篇关于按下应用程序后退按钮时如何确认应用程序退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆