Flutter:水平列表视图上的最小高度 [英] Flutter: Minimum height on horizontal list view
问题描述
我正在尝试在 Flutter 中创建一个水平滚动的项目列表,我希望该列表仅根据其子项占据必要的高度.按照设计,ListView
试图扩展以适应其横向可用的空间"(来自 Flutterdocs),我还注意到它占据了视口的整个高度,但是有没有办法让它不这样做?理想情况下与此类似(显然不起作用):
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
嵌套在 SingleChildScrollView
中的 Column
和 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(),
// ...
],
),
),
],
);
推荐答案
只需将 ListView
的 shrink
属性设置为 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(),
),
这篇关于Flutter:水平列表视图上的最小高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!