How to dismiss an AlertDialog on a FlatButton click?

前端 未结 16 1296
轮回少年
轮回少年 2021-02-03 16:44

I have the following AlertDialog.

showDialog(
            context: context,
            child: new AlertDialog(
              title: const Text(\"Lo         


        
相关标签:
16条回答
  • 2021-02-03 17:08

    Navigator.of(dialogContext).pop() otherwise you can close page if you navigated from Master to Detail page

                    showDialog(
                      context: context,
                      builder: (dialogContext) {
                        return Dialog(
                          child: Column(
                            children: [
                              Text("Content"),
                              RaisedButton(
                                onPressed: () => Navigator.of(dialogContext).pop(),
                                child: Text("Close"),
                              )
                            ],
                          ),
                        );
                      },
                    );
    
    0 讨论(0)
  • 2021-02-03 17:09

    Creating a separate context for Alert Dialog would help.

    showDialog(
      context: context,
      builder: (alertContext) => AlertDialog(
        title: const Text("Location disabled"),
        content: const Text(
            """Location is disabled on this device. Please enable it and try again."""),
        actions: [
          new FlatButton(
            child: const Text("Ok"),
            onPressed: () => Navigator.pop(alertContext),
          ),
        ],
      ),
    );
    
    0 讨论(0)
  • 2021-02-03 17:10

    This worked for me Navigator.of(context, rootNavigator: true).pop('dialog').

    Navigator.pop() just closes the current page/screen.

    0 讨论(0)
  • 2021-02-03 17:12

    Use Navigator.pop(context);

    Example

    showDialog(
                context: context,
                child: new AlertDialog(
                  title: const Text("Location disabled"),
                  content: const Text(
                      """
    Location is disabled on this device. Please enable it and try again.
                      """),
                  actions: [
                    new FlatButton(
                      child: const Text("Ok"),
                      onPressed: () {
                          Navigator.pop(context);
                        },
                    ),
                  ],
                ),
            );
    
    0 讨论(0)
  • 2021-02-03 17:13

    Example of dismissing alert dialog on flat button click

    RaisedButton(
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (context) => AlertDialog(
                        title: Text('Are you sure?'),
                        content: Text('Do you want to remove item?'),
                        actions: <Widget>[
                          FlatButton(
                              onPressed: () => Navigator.of(context).pop(false),//  We can return any object from here
                               child: Text('NO')),
                          FlatButton(
                              onPressed: () => Navigator.of(context).pop(true), //  We can return any object from here
                              child: Text('YES'))
                        ],
                      )).then((value) =>
                  print('Selected Alert Option: ' + value.toString()));
            },
            child: Text('Show Alert Dialog'),
          ),
    

    Above code have two unique things which is used to provide callback result of dialog

    Navigator.of(context).pop(false) -- return false value when we pressed NO Navigator.of(context).pop(true) -- return true value when we pressed YES

    Based on these return value, we can perform some operation outside of it or maintain the dialog status value

    0 讨论(0)
  • 2021-02-03 17:13

    Please use following for code to close dialog

    RaisedButton(
         onPressed: () { Navigator.of(context).pop();},
         child: Text("Close",style: TextStyle(color: Colors.white), ),
                    color: Colors.black,
               )
    
    0 讨论(0)
提交回复
热议问题