listview - Flutter:SimpleDialog 中的 ListView

我想使用以下代码在我的 Flutter 应用中显示一个带有 ListView.builder 的 SimpleDialog:

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

这给出了这个错误(抱歉,我无法将日志包装为代码,因为 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

我尝试使用具有特定高度和宽度的 Container,它可以工作,但我希望 ListView 能够在 Dialog 中适应自身。

如何在 SimpleDialog 中包含 ListView?

最佳答案

只需将 ListView.builder 包装在具有特定 heightwidthContainer 中。

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(),
      );
    });

已编辑:

您也可以使用 @Rap 的 评论。

https://stackoverflow.com/questions/50095763/

相关文章:

google-maps - 如果可以使用 flutter 打开谷歌地图应用程序

gridview - Flutter GridView 页脚(用于指示无限滚动的负载)

android - Flutter - 导航到新屏幕,并清除所有以前的屏幕

android - 如何在 Flutter 中获取 AppBar 高度

ios - 在 Flutter 中打开原生 UIViewController

dart - 使用 Flutter/dart 的 NTLM 身份验证

android - 如何使用 Flutter 构建增强现实应用程序?

dart - 为桌面构建 Flutter 应用程序

flutter - 如何在 flutter 中获得唯一的设备ID?

flutter - 如何在 flutter 小部件测试中捕获来自 future 的错误?