Dan*_*ani 3 flutter flutter-layout
我可以设置最小高度,如下所示:ListView(shrinkWrap:true,children:listItems.toList(),);
但是我怎样才能有最大高度来显示最多 3 个元素呢?(要查看其余部分,我必须滚动)
您可以用 SizedBox 包装 ListView,如下所示:
SizedBox(
height: 300,
child: ListView(
children: <Widget>[
SizedBox(height: 100, child: Placeholder()),
SizedBox(height: 100, child: Placeholder()),
SizedBox(height: 100, child: Placeholder()),
SizedBox(height: 100, child: Placeholder()),
],
),),
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您希望容器在项目数少于 3 时缩小,请为容器和shrinkWrap: trueListView 设置 maxHeight 约束:
Container(
decoration: BoxDecoration(border: Border.all(width: 2.0, color: Colors.red)),
constraints: BoxConstraints(maxHeight: 300),
child: ListView(
shrinkWrap: true,
children: <Widget>[
SizedBox(height: 100, child: Placeholder()),
SizedBox(height: 100, child: Placeholder()),
],
)
),
Run Code Online (Sandbox Code Playgroud)