How to dismiss an AlertDialog on a FlatButton click?

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

I have the following AlertDialog.

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


        
16条回答
  •  粉色の甜心
    2021-02-03 16:57

    If you don't want to return any result, use either of them:

    Navigator.of(context).pop();
    Navigator.pop(context);
    

    But if you do want to return some result, see this

    Example:

    showDialog(
        context: context,
        builder: (_) {
          return AlertDialog(
            title: Text('Wanna Exit?'),
            actions: [
              FlatButton(
                onPressed: () => Navigator.pop(context, false), // passing false
                child: Text('No'),
              ),
              FlatButton(
                onPressed: () => Navigator.pop(context, true), // passing true
                child: Text('Yes'),
              ),
            ],
          );
        }).then((exit) {
      if (exit == null) return;
    
      if (exit) {
        // user pressed Yes button
      } else {
        // user pressed No button
      }
    });
    

提交回复
热议问题