如何在Flutter中使用水平ListView内的ListTile?

3

我正在尝试将ListTiles作为水平ListView的项目。

final brandsWidget = SizedBox(
  height: 200,
  child: ListView(
    scrollDirection: Axis.horizontal,
    children: [
      ListTile(
        leading: Image.asset('img_1.png'),
        title: Text('Product 1'),
        subtitle: Text('\$5'),
      ),
      ListTile(
        leading: Image.asset('img_1.png),
        title: Text('Product 2'),
        subtitle: Text('\$3'),
      )
    ],
  ),
);

我得到了以下错误:

Another exception was thrown: BoxConstraints forces an infinite width.
Exception caught by rendering library.
BoxConstraints forces an infinite width.
The relevant error-causing widget was ListTile 

例如,将ListTile包裹在SizedBox中。 - undefined
如果你的brandsWidget不在一个Row中,那么你需要将你的ListTiles包裹在一个容器中来设置一个定义好的宽度。根据定义,ListTiles没有固定的宽度,并且会无限地延伸,受到约束的影响。 - undefined
ListTile具有无限宽度。将其包裹在其他小部件中,例如容器,并指定特定的宽度。 - undefined
1
在指定宽度的SizedBox中包裹后,它可以正常工作。感谢pskink和Kedar。 - undefined
@ScottGodfrey 它位于一个垂直的 ListView 内部。 - undefined
1个回答

5

您可以通过使用ContainerSizedBox widget将其包装来使用ListTile小部件。

错误说明:

出现此错误是因为ListTiles具有无限宽度。 如果您不给它们适当的宽度,它们肯定会生成此类错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接