Listview 颤振:水平列表视图上的最小高度
我正在尝试创建一个水平滚动列表,列表中的项目在Flutter中,我希望该列表仅根据其子项占据必要的高度。通过设计“Listview 颤振:水平列表视图上的最小高度,listview,dart,flutter,Listview,Dart,Flutter,我正在尝试创建一个水平滚动列表,列表中的项目在Flutter中,我希望该列表仅根据其子项占据必要的高度。通过设计“ListView尝试扩展以适应其交叉方向上的可用空间”(从),我也注意到它占据了视口的整个高度,但有没有办法使它不这样做?理想情况下类似于此(显然不起作用): 新建列表视图( 滚动方向:轴水平, crossAxisSize:crossAxisSize.min, 儿童:[ 新建ListItem(), 新建ListItem(), // ... ], ); 我意识到实现这一点的一种方法
ListView
尝试扩展以适应其交叉方向上的可用空间”(从),我也注意到它占据了视口的整个高度,但有没有办法使它不这样做?理想情况下类似于此(显然不起作用):
新建列表视图(
滚动方向:轴水平,
crossAxisSize:crossAxisSize.min,
儿童:[
新建ListItem(),
新建ListItem(),
// ...
],
);
我意识到实现这一点的一种方法是将
列表视图
包装在具有固定高度的容器中。但是,我不一定知道物品的高度:
新容器(
身高:97.0,
子:新列表视图(
滚动方向:轴水平,
儿童:[
新建ListItem(),
新建ListItem(),
// ...
],
),
);
我能够通过将行
嵌套在SingleChildScrollView
的列
中,并使用mainAxisSize:mainAxisSize.min
来拼凑出一个“解决方案”。然而,对我来说,这并不是一个解决方案:
新列(
mainAxisSize:mainAxisSize.min,
儿童:[
新的SingleChildScrollView(
滚动方向:轴水平,
孩子:新的一排(
儿童:[
新建ListItem(),
新建ListItem(),
// ...
],
),
),
],
);
只需将ListView
的shorn
属性设置为true
,它将适合空间,而不是扩展
例如:
ListView(
shrinkWrap: true, //just set this property
padding: const EdgeInsets.all(8.0),
children: listItems.toList(),
),
使用ConstrainedBox
设置minHeight
和maxHeight
ConstrainedBox(
constraints: new BoxConstraints(
minHeight: 35.0,
maxHeight: 160.0,
),
child: new ListView(
shrinkWrap: true,
children: <Widget>[
new ListItem(),
new ListItem(),
],
),
)
ConstrainedBox(
约束:新的BoxConstraints(
最小身高:35.0,
最大高度:160.0,
),
子:新列表视图(
收缩膜:对,
儿童:[
新建ListItem(),
新建ListItem(),
],
),
)
使用扩展的
Expanded(
child:ListView.separated(
shrinkWrap: true,
padding: EdgeInsets.all(10),
separatorBuilder: (BuildContext context, int index) {
return Align(
alignment: Alignment.centerRight,
child: Container(
height: 0.5,
width: MediaQuery.of(context).size.width / 1.3,
child: Divider(),
),
);
},
itemCount: dataMasterClass.length,
itemBuilder: (BuildContext context, int index) {
Datum datum = dataMasterClass[index];
return sendItem(datum);
},)
) ,
用mainAxisSize:mainAxisSize.min通过Column
包装小部件对我来说很有效。您可以尝试在此处更改高度
var数据=[
{'name':'Shopping','icon':Icons.local_shipping},
{'name':'Service','icon':Icons.room_Service},
{'name':'Hotel','icon':Icons.Hotel},
{'name':'More','icon':Icons.More}
];
新容器(
约束:新的BoxConstraints(
最小高度:40.0,
最大高度:60.0,
),
颜色:颜色。透明,
子:新列表视图(
滚动方向:轴水平,
儿童:数据
.map((e)=>列(
mainAxisSize:mainAxisSize.min,
儿童:[
新容器(
宽度:40,
高度:50,//尝试更改此值。
颜色:颜色。透明,
页边距:仅限边集(右:20,左:4),
孩子:斜坡(
子:容器(
填充:边缘设置。全部(4),
颜色:颜色,白色,
子:图标(
e[“图标”],
颜色:颜色。灰色,
尺码:30,
),
),
),
),
],
))
.toList(),
));
我正在使用容器的水平listview构建器,因此为了阻止容器达到最大高度,我刚刚用中心将其包裹起来,它工作得非常好
itemBuilder: (context, i){
return Center(child: word_on_line(titles[i]));
}
通过应用上述所有解决方案,还没有找到合适的答案,这有助于我们在不知道高度的情况下设置水平Listview。在所有情况下,我们都必须设定高度。因此,我应用了下面的解决方案,该解决方案适用于我,没有指定列表项的任何高度。@sindrenm在他的问题中已经提到了这一点。因此,在找到可行的解决方案之前,我愿意一直坚持下去。我应用了包覆面收缩:true,但它将沿主轴收缩ListView。(仅适用于垂直滚动视图)而不是如问题中所述沿着横轴。因此,在不久的将来,任何人都可以在生产中使用此解决方案,它对我的所有水平列表都非常有用
//below declared somewhere in a class above any method and assume list has filled from some API.
var mylist = List<myModel>();
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
for (int index = 0; index < mylist.length; index++)
listItem(mylist[index]);
],
),
),
),
listItem(){
return Column(
children[
widget1(),
widget2().... etc..
]);
}
//在任何方法之上的类中的某处声明,并假设列表已从某个API填充。
var mylist=List();
SingleChildScrollView(
滚动方向:轴水平,
孩子:填充(
填充:常数边集全部(8.0),
孩子:排(
儿童:[
对于(int index=0;index
据我所知,不能在垂直ListView中设置水平ListView,也不能动态设置其高度。如果您的需求允许(没有无限滚动、元素数量少等),您可以使用SingleChildScrollView
SingleChildScrollView(
滚动方向:轴水平,
孩子:排(
儿童:[……],
),
);
这与此处提出的问题非常相似:
我相信ListView要求每个项目具有相同的横轴大小。这意味着在这种情况下,我们无法为每个对象设置唯一的高度,交叉轴的大小是固定的
如果您想让可滚动文件的高度由子文件本身唯一控制,则可以使用SingleChildScrollView与行搭配使用(注意:确保设置
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: SafeArea(
child: Container(
// height: 100, // no need to specify here
color: Colors.white,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: <Widget>[
Container(
height: 300,
width: 300,
color: Colors.amber[600],
child: const Center(child: Text('Entry A')),
),
Container(
height: 100,
color: Colors.amber[500],
child: const Center(child: Text('Entry B')),
),
Container(
height: 50,
width: 100,
color: Colors.amber[100],
child: const Center(child: Text('Entry C')),
),
Container(
height: 300,
width: 300,
color: Colors.amber[600],
child: const Center(child: Text('Entry A')),
),
Container(
height: 100,
color: Colors.amber[500],
child: const Center(child: Text('Entry B')),
),
Container(
height: 50,
width: 100,
color: Colors.amber[100],
child: const Center(child: Text('Entry C')),
),
Container(
height: 300,
width: 300,
color: Colors.amber[600],
child: const Center(child: Text('Entry A')),
),
Container(
height: 100,
color: Colors.amber[500],
child: const Center(child: Text('Entry B')),
),
Container(
height: 50,
width: 100,
color: Colors.amber[100],
child: const Center(child: Text('Entry C')),
),
Container(
height: 300,
width: 300,
color: Colors.amber[600],
child: const Center(child: Text('Entry A')),
),
Container(
height: 100,
color: Colors.amber[500],
child: const Center(child: Text('Entry B')),
),
Container(
height: 50,
width: 100,
color: Colors.amber[100],
child: const Center(child: Text('Entry C')),
),
Container(
height: 300,
width: 300,
color: Colors.amber[600],
child: const Center(child: Text('Entry A')),
),
Container(
height: 100,
color: Colors.amber[500],
child: const Center(child: Text('Entry B')),
),
Container(
height: 50,
width: 100,
color: Colors.amber[100],
child: const Center(child: Text('Entry C')),
),
],
),
)),
),
),
);
}
}
Widget _horizontalWrappedRow(List data) {
var list = <Widget>[Container(width: 16)]; // container is left padding
//create a new row widget for each data element
data.forEach((element) {
list.add(MyRowItemWidget(element));
});
// add the list of widgets to the Row as children
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: list,
),
);
}