警报框中的导航器无法正常工作

flutter navigator in alert box not working

在编辑页面中,一旦编辑完成,我就会显示带有“确定”按钮的警报。单击“确定”按钮后,我想移至上一个屏幕。以下是我的警告框代码

Future<Null> _showDoneDialog() async {
    return showDialog<Null>(
      context: context,
      //barrierDismissible: true,
      builder: (BuildContext context) {
        return new AlertDialog(
          title: Center(
            child: SingleChildScrollView(
              child: Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                        margin: EdgeInsets.only(top: 12),
                        child: Text(
                          "Profile has been edited successfully",
                          textAlign: TextAlign.start,
                          style: TextStyle(
                              color: Colors.black54,
                              fontSize: 14,
                              fontFamily: 'Oswald',
                              fontWeight: FontWeight.w400),
                        )),
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              setState(() {
                                Navigator.of(context).pop();
                              });
                            },
                            child: Container(
                                decoration: BoxDecoration(
                                    color: header,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(100))),
                                child: Text(
                                  "OK",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 17,
                                  ),
                                  textAlign: TextAlign.center,
                                )),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

如何从警报框调用上一个屏幕 -> Navigator.of(context).pop();

showDialog 使用导航器显示。因此,如果您的导航器堆栈是

屏幕 1>屏幕 2

当您显示对话框时,它会变成

屏幕 1>屏幕 2>FlutterDialog

并且...当您“弹出”导航器时,它会弹出最后一条路线,在这种情况下,它会移动到上一个屏幕Screen2

我建议等待对话框弹出,然后再次“弹出”(转到屏幕 1)

您可以通过简单的方式完成此操作 manner.Just 等待对话框自行关闭

void _showDoneDialog() async {
    await showDialog<Null>(
      context: context,
      //barrierDismissible: true,
      builder: (BuildContext dialogContext) {
        return new AlertDialog(
          title: Center(
            child: SingleChildScrollView(
              child: Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                        margin: EdgeInsets.only(top: 12),
                        child: Text(
                          "Profile has been edited successfully",
                          textAlign: TextAlign.start,
                          style: TextStyle(
                              color: Colors.black54,
                              fontSize: 14,
                              fontFamily: 'Oswald',
                              fontWeight: FontWeight.w400),
                        )),
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              setState(() {
                                Navigator.of(dialogContext).pop();
                              });
                            },
                            child: Container(
                                decoration: BoxDecoration(
                                    color: header,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(100))),
                                child: Text(
                                  "OK",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 17,
                                  ),
                                  textAlign: TextAlign.center,
                                )),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );


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