【问题标题】:How to dismiss alert dialog when clicked outside of it在外部单击时如何关闭警报对话框
【发布时间】:2020-02-04 14:20:01
【问题描述】:

我有一个警报对话框,我想在外部单击时将其关闭,我知道这是颤振中警报对话框的默认行为,但我不知道在外部单击时阻止它关闭的问题是什么.

我尝试将 barrierDismissable 设置为 true,但它仍然不起作用。

This is my dialog : 

termsAndConditionsDialog(BuildContext context) {

    AlertDialog alert = AlertDialog(
      title:  Text("Terms and Conditions", style: TextStyle(fontSize: 18, color: AppColors.accentColor),),
      content: Text(
        generalSettings.policyForCustomer,
        style: TextStyle(fontSize: 16),
      ),

    );


    // show the dialog
    showDialog(
      barrierDismissible: true,
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }


and this is how I call it from button's onPressed : 
 termsAndConditionsDialog(context);

【问题讨论】:

  • 您的代码是正确的,我尝试过,当我点击外部时对话框会消失。可以分享完整代码吗?
  • 我认为问题是我在 MyApp 中有一个手势检测器,用于加载小部件,当我删除它时,一切正常,有没有办法可以保留手势检测器并且还可以关闭对话框? builder: (BuildContext context, Widget child) { return GestureDetector( onTap: ()=>FocusScope.of(context).requestFocus(FocusNode()), child: LoadingProvider( child: Directionality(textDirection:TextDirection.rtl, child: child), )); },

标签: flutter dialog android-alertdialog flutter-layout flutter-alertdialog


【解决方案1】:

自定义警报方法

typedef ResponseCallbackValueLess = void Function();

showAlertDialogWithOkButton(
  BuildContext context,
  String message,
  String heading,
  String buttonAcceptTitle,
  ResponseCallbackValueLess callback) {
Widget continueButton = FlatButton(
  child: Text(buttonAcceptTitle),
  onPressed: () {
    callback();
  },
);

如下调用:

showAlertDialogWithOkButton(context,
                          'No items found in your cart.',
                          "app name", "Ok", dismissAlert(context));

dismissAlert(context){
    Navigator.pop(context);
  }

【讨论】:

    【解决方案2】:

    在 onPressed 或 onTap 时调用它:

    void showMessage(){
      showDialog(
        context:context,
        builder: (Buildcontext context){
           return AlertDialog(
             backgroundColor: Colors.transparent,
             content: Container(
                width: MediaQuery.of(context).size.width,
                height: 100,
                alignment: AlignmentDirectional.center
                child: Text("TRIAL",style: TextStyle(color: Colors.white))
            )
          )
        }
      )
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 2016-07-16
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多