1

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);
Ghada Shebl
  • 539
  • 2
  • 6
  • 12
  • Your code is right, i tried and dialog dismissing when i click outside. Can you share full code? – ibrahimkarahan Oct 07 '19 at 10:44
  • I think the problem is that I have a Gesture Detector in MyApp, which I use for the loading widget, when I remove it, everything works fine, is there a way that I can keep the gesture detector and also make the dialogs dismissable ? ```builder: (BuildContext context, Widget child) { return GestureDetector( onTap: ()=>FocusScope.of(context).requestFocus(FocusNode()), child: LoadingProvider( child: Directionality(textDirection:TextDirection.rtl, child: child), )); },``` – Ghada Shebl Oct 08 '19 at 18:14

2 Answers2

0

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))
        )
      )
    }
  )
}
Z. Cajurao
  • 357
  • 2
  • 12
0

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);
  }
Anil Kumar
  • 1,830
  • 15
  • 24