简体   繁体   中英

How to dismiss alert dialog when clicked outside of it

I have an alert dialog that I want to be dismissed when clicked outside of it, I know this is the default behavior of alert dialogs in flutter, but I don't know what's the problem that's preventing it from closing when clicked outside.

I tried to use barrierDismissable to true, but still, it doens't work.

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

call this upon onPressed or 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))
        )
      )
    }
  )
}

Custom Alert method

typedef ResponseCallbackValueLess = void Function();

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

called like below:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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