繁体   English   中英

Flutter:willpopScope 在 appBar 后退按钮中不起作用

[英]Flutter : willpopScope is not working in appBar back button

这里我使用会弹出 scope。 但在 appBar 后退按钮 WillPopScope 不起作用。

在这里,当 stockListData 不为空时,任何人都想 go 返回上一页然后我想要 willPOPScope 对话框消息,这里这对于设备后退按钮工作正常,但我也希望在 appBar 后退按钮上也有同样的东西。 如何使它可行?


在这里,当 stockListData 不为空时,任何人都想 go 返回上一页然后我想要 willPOPScope 对话框消息,这里这对于设备后退按钮工作正常,但我也希望在 appBar 后退按钮上也有同样的东西。

这是我的代码。

return WillPopScope(
      onWillPop: () async {
        if (selectedIndex == 3 && stockListData.length != 0) {
          final value = await showDialog<bool>(
              context: context,
              builder: (context) {
                // Widget cancelButton = FlatButton(
                //   color: Colors.black,
                //   child: Text(
                //     "No",
                //     style: TextStyle(color: Colors.white),
                //   ),
                //   onPressed: () {
                //     Navigator.of(context).pop(false);
                //   },
                // );
                Widget continueButton = FlatButton(
                    color: Colors.black,
                    child: Text(
                      "OK",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop(true);
                    });

                return AlertDialog(
                  titlePadding: EdgeInsets.only(top: 13),
                  title: Container(
                    height: 40,
                    child: Center(
                      child: Text(
                        "Alert",
                        style: TextStyle(color: blackColor),
                      ),
                    ),
                    // color: Color(0xff007669),
                  ),
                  content: Text(
                    "If you leave this page then your added stock will be deleted.",
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 12),
                  ),
                  actions: [
                    // cancelButton,
                    continueButton,
                  ],
                  actionsAlignment: MainAxisAlignment.center,
                );
              });
          return value == true;
        } else {
          setState(() {Navigator.of(context).pop(true);});
          return false;
        }
      },
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text(
            "Portfolio Review",
            style: TextStyle(fontSize: tSize16),
          ),
          leading: Builder(
            builder: (BuildContext context) {
              return IconButton(
                icon: Icon(Icons.arrow_back_ios_rounded),
                onPressed: () {
                  Navigator.pop(context);
                },
                tooltip: '',
              );
            },
          ),
          elevation: 0,
          backgroundColor: skyBlue,
        ),
        body: SingleChildScrollView());

你有没有在 App-bar 中自动添加ImplyLeading: false

在应用栏中,您直接弹出屏幕。 提取将弹出scope的方法

            builder: (BuildContext context) {
              return IconButton(
                icon: Icon(Icons.arrow_back_ios_rounded),
                onPressed: () {
                  Navigator.pop(context);//call the extracted method here
                },
                tooltip: '',
              );
            },
          ),

代替 navigator.pop 使用提取的方法..

也只是建议您不必设置 navigator.pop 或 navigator.push。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM