简体   繁体   English

没有明确高度的水平 ListView 颤动

[英]Horizontal ListView flutter WITHOUT explicit height

I'm trying to create a Horizontal scrolling listview.builder() with no pre-set height.我正在尝试创建一个没有预设高度的水平滚动listview.builder()

I've tried setting shrinkwrap to true and wrapping it inside an Expanded/Flexible.我试过将shrinkwrap设置为true 并将其包装在Expanded/Flexible 中。

The only way (that i have found) to currently achieve the desired effect is to wrap a row inside a singlechildscrollview inside a column, as per this answer ( Flutter: Minimum height on horizontal list view ).根据这个答案( 颤振:水平列表视图上的最小高度),目前实现预期效果的唯一方法(我发现)是将一行包裹在一个列内的singlechildscrollview

The problem with that method is that there is no builder method to load dynamic data into the Cards inside the singlechildscrollview .该方法的问题在于没有构建器方法将动态数据加载到singlechildscrollview内的 Cards 中。

My question is how do i create a Horizontal listview that that generates the output by the row nested inside the singlechildscrollview ( Flutter: Minimum height on horizontal list view ) but with a builder method?我的问题是如何创建一个水平listview ,该listview通过嵌套在singlechildscrollviewrow生成输出( 颤振:水平列表视图上的最小高度)但使用构建器方法?

With Flexible灵活的

Scaffold(
  body: Container(
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Flexible(
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: 3,
            itemBuilder: (BuildContext context, int index) {
              return FeaturedCard();
            },
          ),
        ),
        Flexible(
          child: ListView.builder(
            shrinkWrap: true,
            itemCount: 10,
            itemBuilder: (BuildContext context, int index) {
              return FeaturedCard();
            },
          ),
        ),
      ],
    ),
  ),
)

Result : https://i.stack.imgur.com/XKiWo.jpg结果https : //i.stack.imgur.com/XKiWo.jpg

With nested row inside singlechildscrollview (The method that works)singlechildscrollview嵌套row (有效的方法)

 Container(
  padding: EdgeInsets.only(top: 16, bottom: 8),
  child: Column(
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Row(
          children: <Widget>[
            FeaturedCard(),
            FeaturedCard(),
          ],
        ),
      ),
    ],
  ),
)

Result: https://i.stack.imgur.com/va3TY.jpg结果: https : //i.stack.imgur.com/va3TY.jpg

Notice the added space inside the card when using flexible (this actually renders worse on different devices)注意使用灵活时卡内增加的空间(这实际上在不同的设备上变得更糟)

The Flutter framework can only know the height of a widget once it's been built. Flutter 框架只有在构建后才能知道小部件的高度。

If you're building ListView children dynamically, it can't calculate the required height of the ListView until all it's children have been built, which might never happen (infinite ListView ).如果您正在动态构建ListView子项,则在构建所有子项之前,它无法计算ListView所需的高度,这可能永远不会发生(无限ListView )。

You can either give the ListView a fixed height and build its children dynamically or have the ListView's height depend on it's children, in which case you'd need to build all it's children upfront.您可以给ListView一个固定的高度并动态构建其子项,或者让ListView's高度取决于它的子项,在这种情况下,您需要预先构建它的所有子项。

Posting answer for OP who edited their answer into their question为将答案编辑到问题中的 OP 发布答案

Solved the problem by creating a custom builder method like so:通过创建自定义构建器方法解决了这个问题,如下所示:

Widget _buildFeaturedCards(List<Product> product) {
  final cards = <Widget>[];
  Widget FeautredCards;

  if (product.length > 0) {
    for (int i = 0; i < product.length; i++) {
      cards.add(FeaturedCard(product[i]));
      print(product.length);
    }
    FeautredCards = Container(
      padding: EdgeInsets.only(top: 16, bottom: 8),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(children: cards),
          ),
        ],
      ),
    );
  } else {
    FeautredCards = Container();
  }
  return FeautredCards;
}

This creates the necessary scrolling widgets upfront instead of lazily like ListView.builder would.这会预先创建必要的滚动小部件,而不是像ListView.builder那样懒惰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM