SimpleDialog不显示在Flutter中

时间:2019-08-29 09:00:57

标签: flutter dart simpledialog

我正在使用Flutter开发应用程序。满足特定条件后,我需要显示一个对话框。完成后,将不会显示该对话框,但屏幕会变暗,就像正在显示该对话框一样。

showEndGamePopUp() {
    showDialog<void>(
      context: context,
      builder: (_) {
        return Container(
          child: SimpleDialog(
            backgroundColor: Colors.black,
            elevation: 2.0,
            title: Text(
              "$playerTurn wins!",
              style: TextStyle(
                color: Colors.white,
                fontSize: 20.0,
                height: 1.5,
              ),
            ),
            children: <Widget>[
              SimpleDialogOption(
                  onPressed: () => Navigator.pop(context),
                  child: Text("Play again"),
              ),
              SimpleDialogOption(
                  onPressed: () => exit(0),
                  child: Text("Exit"),
              ),
            ],
          ),
        );
      },
    );
  }

我得到以下异常:RenderBox was not laid out: RenderCustomPaint#3d792 relayoutBoundary=up3 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UP

2 个答案:

答案 0 :(得分:0)

问题在于您使用Expanded

我修复了您的代码。在这里。

showEndGamePopUp() {
    showDialog<void>(
      context: context,
      builder: (_) {
        return Container(
          child: SimpleDialog(
            backgroundColor: Colors.red,
            elevation: 2.0,
            title: Text(
              "wins!",
              style: TextStyle(
                color: Colors.white,
                fontSize: 20.0,
                height: 1.5,
              ),
            ),
            children: <Widget>[
              Row(children: <Widget>[
                Expanded(
                    child: SimpleDialogOption(
                  onPressed: () => Navigator.pop(context),
                  child: Text("Play again"),
                )),
              ]),
              Row(
                children: <Widget>[
                  Expanded(
                    child: SimpleDialogOption(
                      onPressed: () => print(0),
                      child: Text("Exit"),
                    ),
                  )
                ],
              ),
            ],
          ),
        );
      },
    );
  }

只需使用Row包装您的Expanded

如果愿意,可以使用Column包装Expanded

Expanded必须直接放在flex小部件内。

答案 1 :(得分:0)

    showEndGamePopUp(BuildContext context) {
        showDialog<void>(
        context: context,
        builder: (_) {
            return Container(
            child: SimpleDialog(
                backgroundColor: Colors.black,
                elevation: 2.0,
                title: Text(
                "wins!",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 20.0,
                    height: 1.5,
                ),
                ),

                children: <Widget>[
                new FlatButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    child: new Text("Play again",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 20.0,
                            height: 1.5,
                        ))),
                new FlatButton(
                    onPressed: () {
                        Navigator.of(context).pop(true);
                    },
                    child: new Text("Exit",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 20.0,
                            height: 1.5,
                        )))
                ],
            ),
            );
        },
        );
    }

enter image description here