7
votes

I have a strange problem with AlertDialog in flutter to dismiss the dialog. I was using the below code snippet to close the dialog as mentioned in the flutter documentation.

 Navigator.of(dialogContext).pop();

But show how it doesn`t work and make the app into the inactive mode and turns into the black screen window. To make it work again, i have to kill the app and restart again.

Here is the complete code for alertdialog in flutter

Future<Null> _showDialogContactDial(context, Contact contactRecord) async {

  return showDialog<Null>(
    context: context,
    barrierDismissible: true, // user must tap button!
    builder: (BuildContext dialogContext) {
      return new AlertDialog(
        title: new Text('Confirm Number'),
        content: new SingleChildScrollView(
          child: new ListBody(
            children: <Widget>[
              new TextFormField(
                maxLines: 1,
                decoration: new InputDecoration(hintText: 'Number'),
                keyboardType: TextInputType.number,
                autofocus: false,
                initialValue: contactRecord.phoneNumber.number,
              ),
            ],
          ),
        ),
        actions: <Widget>[
          new FlatButton(
            child: new Text(
              'Call',
              style: TextStyle(color: Colors.black),
            ),
            onPressed: () {
              Navigator.of(dialogContext).pop();


              _launchURL(
                  context);
            },
          ),
          new FlatButton(
            color: Colors.red,
            child: new Text('Close', style: TextStyle(color: Colors.white)),
            onPressed: () {
              Navigator.of(dialogContext).pop();
            },
          ),
        ],
      );
    },
  );
}

I also noticed that it works for one button "call" without any issues but not for the cancel alert dialog as you see in the same code snippet in both button actions.

dialog after the dialogdismiss app Help would be appreciated.

3
Added screenshots for referenceNagendra Badiganti
The issue here is not AlertDialogBox - It has to do with your Parent widget from where it is called. your Code Works Fine. Both for Call & Close Action Widgets.anmol.majhail

3 Answers

14
votes

Just add rootNavigator:true

Navigator.of(dialogcon, rootNavigator: true).pop();
0
votes

This worked in my application , i have made little changes in your code ,hope this might help , if this doesn't help you then I think here is a problem in _launchURl method.

void _showDialogContactDial(BuildContext context, Contact contactRecord){

showDialog<Null>(
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext dialogContext) {
  return new AlertDialog(
    title: new Text('Confirm Number'),
    content: new SingleChildScrollView(
      child: new ListBody(
        children: <Widget>[
          new TextFormField(
            maxLines: 1,
            decoration: new InputDecoration(hintText: 'Number'),
            keyboardType: TextInputType.number,
            autofocus: false,
            initialValue: contactRecord.phoneNumber.number,
          ),
        ],
      ),
    ),
    actions: <Widget>[
      new FlatButton(
        child: new Text(
          'Call',
          style: TextStyle(color: Colors.black),
        ),
        onPressed: () {
          Navigator.of(dialogContext).pop();


          _launchURL(
              context);
        },
      ),
      new FlatButton(
        color: Colors.red,
        child: new Text('Close', style: TextStyle(color: Colors.white)),
        onPressed: () {
          Navigator.of(dialogContext).pop();
        },
      ),
    ],
  );
},

); }

Use this method as a callback for onTap or wherever you are using it.

0
votes

Inside your dialog. Surround your flatbuttons with Builder.

Builder(
          builder: (context) => FlatButton(
            child: Text('Cancelar'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ),