颤振:水平列表视图上的最小高度 [英] Flutter: Minimum height on horizontal list view
问题描述
我正在尝试在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(),
// ...
],
),
),
],
);
推荐答案
只需将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(),
),
这篇关于颤振:水平列表视图上的最小高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!