Flutter:SimpleDialog中的ListView [英] Flutter: ListView in a SimpleDialog

查看:487
本文介绍了Flutter:SimpleDialog中的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show a SimpleDialog with ListView.builder in my Flutter app with this code:

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抱怨代码太多):

which gives this error (sorry, I couldn't wrap the logs as code because Stackoverflow complains that there's too much code):

══╡渲染库引起的异常CA ══════════════════════ I/flutter(4481):在performLayout()期间引发了以下断言: I/flutter(4481):RenderViewport不支持返回固有尺寸. I/flutter(4481):计算固有尺寸将需要实例化视口的每个子代, I/flutter(4481):消除了视口变得懒惰的观点. I/flutter(4481):如果您只是想在主轴方向上收缩视口,请考虑 I/flutter(4481):RenderShrinkWrappingViewport渲染对象(ShrinkWrappingViewport小部件),它实现了 I/flutter(4481):在未实现固有维度API的情况下生效. I/颤振(4481): ... I/flutter(4481):引发了另一个异常:未布置RenderBox:RenderPhysicalShape#83d92 relayoutBoundary = up2 NEEDS-PAINT I/flutter(4481):引发了另一个异常:'package:flutter/src/rendering/shifted_box.dart':失败的断言:第310行pos 12:'child.hasSize':不正确. I/flutter(4481):引发了另一个异常:未布置RenderBox:RenderPhysicalShape#83d92 relayoutBoundary = up2

══╡ 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能够适合对话框.

I tried using Container with specific height and width, and it works, but I want the ListView to fit itself in the Dialog.

如何在SimpleDialog中包括ListView?

推荐答案

只需将ListView.builder包裹在具有特定高度宽度Container中.

Just wrap ListView.builder in a Container with a specific height and width.

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

您也可以添加 @Rap的 评论.

You can go with @Rap's comment too.

这篇关于Flutter:SimpleDialog中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆