Flutter 如何在另一个页面上显示对话框

Flutter How Can I Show Dialog on Another Page

我想在按下按钮时显示对话框我想导航到另一个页面,然后我想显示一个对话框指示我已导航到该页面。

所以我试试这个代码;

InkWell(
                          onTap: () async {
                            Navigator.pushAndRemoveUntil(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => BottomBarD()),
                                (route) => false);
                            Dialog(
                              child: Container(
                                child: Column(
                                  children: [
                                    Text(
                                      "Navigated to Another Page",
                                      style: TextStyle(
                                        fontFamily: "Quando",
                                        fontWeight: FontWeight.w500,
                                        fontSize: 15,
                                      ),
                                    ),
                                    TextButton(
                                      onPressed: () async {
                                        Navigator.of(context).pop();
                                      },
                                      child: Text(
                                        "Okey",
                                        style: TextStyle(
                                          fontFamily: "Quando",
                                          fontSize: 15,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            );
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              gradient: LinearGradient(
                                  colors: [Colors.orangeAccent, Colors.red],
                                  begin: Alignment.bottomRight,
                                  end: Alignment.centerLeft),
                              borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(112.0),
                                topLeft: Radius.circular(112.0),
                                bottomRight: Radius.circular(112.0),
                                topRight: Radius.circular(112.0),
                              ),
                            ),
                            height: MediaQuery.of(context).size.height * 0.065,
                            width: MediaQuery.of(context).size.width * 0.80,
                            margin: EdgeInsets.only(
                              top: 30.0,
                              bottom: 10.0,
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Container(
                                  child: Text(
                                    "Navigate and ShowDialog Button",
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 17,
                                        fontFamily: "Quando"),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),

但是当我在对话框中按下确定按钮时,确定按钮不起作用,并给出了那个错误;

未处理的异常:对空值使用了空检查运算符

这在 DartPad 上运行良好:

  void _incrementCounter() {
    Navigator.of(context).push(MaterialPageRoute(
      builder: (c) => Scaffold(body: Text('new page'))
    ));
    showDialog(context: context, 
      builder: (c) => AlertDialog(content: Text('the dialog in it'),)
    );
  }

注意 .push() returns 立即成为 Future。您之后显示的对话框显示在第 2 页的顶部,因为我们不等待它。

我将对话框放在我的第二页 BottomBarD() 并在每次 BottomBarD() 加载时根据条件字段显示它。