首页 > 解决方案 > 如何在 Flutter 的水平 ListView 中使用 ListTile?

问题描述

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

标签: flutter

解决方案


您可以通过使用或ListTile包装它来使用小部件ContainerSizedBox widget.

错误说明:

您收到此错误是因为ListTileshave infinite width。如果你不给他们一个合适的,他们肯定会产生这样的错误。


推荐阅读