簡體   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