如何在Navigator.push后关闭AlertDialog?

时间:2020-10-10 16:12:04

标签: flutter dart flutter-layout

在用户按下AlertDialog上的按钮后,我将调用Navigator.push()。但是,当用户按下AlertDialog按钮时,它保持打开状态并位于新页面的顶部。

用户按下按钮后如何关闭AlertDialog?

Future<void> _showMyDialog() async {
          return showDialog<void>(
            context: context,
            barrierDismissible: false, // user must tap button!
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('AlertDialog Title'),
                content: SingleChildScrollView(
                  child: ListBody(
                    children: <Widget>[
                      Text('This is a demo alert dialog.'),
                      Text('Would you like to approve of this message?'),
                    ],
                  ),
                ),
                actions: <Widget>[
                  FlatButton(
                    child: Text('Approve'),
                    onPressed: () async {

                       await Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => Page()),
                      );


                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            },
          );
        }

        await _showMyDialog();

1 个答案:

答案 0 :(得分:0)

评论中称呼流行音乐可能是最简单的方法。

接下来要考虑的另一件事是,如果您希望他们能够保持在同一页面上。如果您超出了应用程序的=> NewPage()导航风格,则可以采用这两种方法。当然,它更常用于抽屉。

祝您编程愉快!


onTap: () {
  newRouteName = "/form_check";
  // if the current route is the exact location we're at (first on the stack), mark that
  Navigator.popUntil(context, (route) {
    if (route.settings.name == newRouteName) {
      isNewRouteSameAsCurrent = true;
    } else {
      isNewRouteSameAsCurrent = false;
    }
    return true;
  });
  // if it isn't, go to the new route
  if (!isNewRouteSameAsCurrent) {
    Navigator.pushNamed(context, newRouteName);
  }
  // again if it is, just pop the drawer/dialog away
  else {
    Navigator.pop(context);
  }
}