簡體   English   中英

如何為 AlertDialog flutter 禁用 onBackPressed()

[英]How to disable onBackPressed() for AlertDialog flutter

我有一個AlertDialog() ,它的barrierDismissible已設置為false 但是,當用戶按下 android 設備上的后退按鈕時,AlertDialog 將關閉。 如何完全阻止用戶關閉 AlertDialog()?

這是我到目前為止所做的:

          return showDialog<bool>(
            context: context,
            barrierDismissible: false,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Title'),
                content: Text('This is the alert dialog content'),
                actions: <Widget>[
                  FlatButton(
                    child: Text('ok'),
                    onPressed: () {
                      Navigator.of(context).pop();
                      print('ok you win');
                    },
                  ),
                ],
              );
            },
          );

嘗試這種方式使用WillPopScope()來處理onBackPressed()事件

showDialog(
                            context: context,
                            barrierDismissible: false,
                            builder: (BuildContext context) {
                              return WillPopScope(
                                onWillPop: () {return Future.value(false);},
                                child:  return AlertDialog(
                title: Text('Title'),
                content: Text('This is the alert dialog content'),
                actions: <Widget>[
                  FlatButton(
                    child: Text('ok'),
                    onPressed: () {
                      Navigator.of(context).pop();
                      print('ok you win');
                    },
                  ),
                ],
              ),
                              );
                            });

您可以將 WillPopScope 用作 AlertDialog 的父級

  showDialog(context: context,builder: (BuildContext context)
          {
              return WillPopScope(
                onWillPop: () async =>false,
                              child: AlertDialog(
                  title: Text('Title'),
                  content: Text('Sample'),
                  actions: <Widget>[
                    FlatButton(
                      child: Text('ok'),
                      onPressed: (){
                        Navigator.of(context).pop();
                      },
                    )
                  ],
                ),
              );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM