简体   繁体   English

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

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

Here I am using will pop scope.这里我使用会弹出 scope。 But in appBar back button WillPopScope is not working.但在 appBar 后退按钮 WillPopScope 不起作用。

here when stockListData is not empty then anyone want to go back previous page then i want willPOPScope dialog message, here This is working fine for device back button but i Want same thing on appBar back button too.在这里,当 stockListData 不为空时,任何人都想 go 返回上一页然后我想要 willPOPScope 对话框消息,这里这对于设备后退按钮工作正常,但我也希望在 appBar 后退按钮上也有同样的东西。 How to make it workable?如何使它可行?


here when stockListData is not empty then anyone want to go back previous page then i want willPOPScope dialog message, here This is working fine for device back button but i Want same thing on appBar back button too.在这里,当 stockListData 不为空时,任何人都想 go 返回上一页然后我想要 willPOPScope 对话框消息,这里这对于设备后退按钮工作正常,但我也希望在 appBar 后退按钮上也有同样的东西。

This is my code.这是我的代码。

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

have you addd automaticallyImplyLeading: false in App-bar你有没有在 App-bar 中自动添加ImplyLeading: false

In the app bar you are directly popping the screen.在应用栏中,您直接弹出屏幕。 Extraxt the method of will pop scope提取将弹出scope的方法

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

And in place of navigator.pop use that extracted method..代替 navigator.pop 使用提取的方法..

Also just a suggestion you dont have to setstate a navigator.pop or navigator.push.也只是建议您不必设置 navigator.pop 或 navigator.push。

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

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