颤振:水平列表视图上的最小高度 [英] Flutter: Minimum height on horizontal list view

查看:74
本文介绍了颤振:水平列表视图上的最小高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Flutter中创建项目的水平滚动列表,并且我希望该列表仅基于子元素占据必要的高度.按照设计,"ListView尝试扩展以适应其横向的可用空间"(来自 Flutter文档 ),我还注意到它占据了视口的整个高度,但是有没有办法使它不做呢?理想情况下,与此类似(显然不起作用):

I'm trying to create a horizontal scrolling list of items in Flutter, and I want that list to only take up the necessary height based on its children. By design "ListView tries to expand to fit the space available in its cross-direction" (from the Flutter docs), which I also notice in that it takes up the whole height of the viewport, but is there a way to make it not do this? Ideally something similar to this (which obviously doesn't work):

new ListView(
  scrollDirection: Axis.horizontal,
  crossAxisSize: CrossAxisSize.min,
  children: <Widget>[
    new ListItem(),
    new ListItem(),
    // ...
  ],
);


我意识到一种实现方法是将ListView包裹在具有固定高度的Container中.但是,我不一定知道这些物品的高度:


I realize that one way to do this is by wrapping the ListView in a Container with a fixed height. However, I don't necessarily know the height of the items:

new Container(
  height: 97.0,
  child: new ListView(
    scrollDirection: Axis.horizontal,
    children: <Widget>[
      new ListItem(),
      new ListItem(),
      // ...
    ],
  ),
);


我可以通过将Row嵌套在Column中的SingleChildScrollView中与mainAxisSize: MainAxisSize.min嵌套在一起的解决方案".但是,对我来说,这似乎不是解决方案:


I was able to hack together a "solution" by nesting a Row in a SingleChildScrollView in a Column with a mainAxisSize: MainAxisSize.min. However, this doesn't feel like a solution, to me:

new Column(
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[
    new SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: new Row(
        children: <Widget>[
          new ListItem(),
          new ListItem(),
          // ...
        ],
      ),
    ),
  ],
);

推荐答案

只需将ListViewshrink属性设置为true,它将适合空间而不是扩展.

Just set shrink property of ListView to true and it will fit the space rather than expanding.

示例:

ListView(
        shrinkWrap: true, //just set this property
        padding: const EdgeInsets.all(8.0),
        children: listItems.toList(),
      ),

这篇关于颤振:水平列表视图上的最小高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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