我想用ListView显示一个SimpleDialog.使用以下代码在我的Flutter 应用程序中创建:

showDialog(
  context: context,
  builder: (BuildContext context) {
    return new SimpleDialog(
      children: <Widget>[
        new FittedBox(
          child: new ListView(
            children: <Widget>[
              new Text("one"),
              new Text("two"),
            ],
          ),
        )
      ],
    );
  },
);

这会产生此错误(抱歉,我无法将日志(log)包装为代码,因为Stackoverflow抱怨代码太多):

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════ I/flutter ( 4481): The following assertion was thrown during performLayout(): I/flutter ( 4481): RenderViewport does not support returning intrinsic dimensions. I/flutter ( 4481): Calculating the intrinsic dimensions would require instantiating every child of the viewport, which I/flutter ( 4481): defeats the point of viewports being lazy. I/flutter ( 4481): If you are merely trying to shrink-wrap the viewport in the main axis direction, consider a I/flutter ( 4481): RenderShrinkWrappingViewport render object (ShrinkWrappingViewport widget), which achieves that I/flutter ( 4481): effect without implementing the intrinsic dimension API. I/flutter ( 4481): ... I/flutter ( 4481): Another exception was thrown: RenderBox was not laid out: RenderPhysicalShape#83d92 relayoutBoundary=up2 NEEDS-PAINT I/flutter ( 4481): Another exception was thrown: 'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 310 pos 12: 'child.hasSize': is not true. I/flutter ( 4481): Another exception was thrown: RenderBox was not laid out: RenderPhysicalShape#83d92 relayoutBoundary=up2

我try 使用具有特定高度和宽度的容器,它可以工作,但我希望ListView能够适合对话框.

How to include a ListView in a SimpleDialog?

推荐答案

只需将ListView.builder包在一个Container中,用特定的heightwidth即可.

Widget setupAlertDialoadContainer() {
  return Container(
    height: 300.0, // Change as per your requirement
    width: 300.0, // Change as per your requirement
    child: ListView.builder(
      shrinkWrap: true,
      itemCount: 5,
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          title: Text('Gujarat, India'),
        );
      },
    ),
  );
}

并在showDialog中调用上述方法.

showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Country List'),
        content: setupAlertDialoadContainer(),
      );
    });

EDITED:

你也可以 Select 100条 comments .

Flutter相关问答推荐

具有可变高度小部件的SingleChildScrollView内的列

如何在Flutter 屏幕中添加箭头形状

Android NFC未收到空的NFC标签

如何将 colored颜色 阴影作为默认容器 colored颜色 应用于自定义窗口小工具?

Flutter OpenID Connect无效参数:redirect_uri using Keycloak

在LinkedIn上分享Fighter和Web的链接会产生不同的结果

摆动更改标记可轻敲文本按钮样式

如何在Flutter 中使用滚动展开窗口小部件

构建方法中的性能声明小部件

按一下按钮即可更新 fl_chart

带按钮的 Riverpod future Provider

创建混合 TextWidget 和 TextFieldWidget

用户文档 ID 与 Firestore 中的用户 ID 相同

FutureProvider 不返回数据给 UI

Flutter url_launcher 插件抛出java.lang.IllegalArgumentException:接收器未注册:io.flutter.plugins.urllauncher.WebViewActivity

Flutter 动保存新的变量值

我想在一个屏幕上有两个相同区域的列表视图,我该如何设置?

Firebase 中的查询限制 - .orderBy() 错误

如何制作两个显示相关内容且可编辑的文本字段?

将一个 Flutter 应用程序集成到另一个 Flutter 应用程序中