【问题标题】:SimpleDialog not displaying in Flutter在 Flutter 中不显示 SimpleDialog
【发布时间】:2025-12-05 19:05:01
【问题描述】:

我正在使用 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

【问题讨论】:

  • 您是否考虑过限制扩展小部件的尺寸?参考this
  • 可以分享完整的sn-ps代码吗?
  • @tomerpacific,这行得通。当我第一次看到这个答案时,我发现我找错了地方。谢谢!
  • 对于任何正在寻找的人来说,设置容器大小对我来说都是诀窍。

标签: flutter dart simpledialog


【解决方案1】:

问题是你使用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 wrap Expanded

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

【讨论】:

    【解决方案2】:
        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,
                            )))
                    ],
                ),
                );
            },
            );
        }
    

    【讨论】: