当scrollDirection设置为垂直时,它按预期工作。问题是,当我将section.axis设置为Axis.horizontal时,ListView以水平方式显示小部件。
使用Flexible或Expanded小部件无法解决此问题,因为ListView的高度需要由列表中的小部件定义。
正如您所看到的,shrinkWrap也已启用。所以我不知道出了什么问题,在此感谢您的帮助。
控制台显示: 'constraints.hasBoundedHeight':不为真。 相关引起错误的小部件是:ListView
class SectionWidget extends StatelessWidget {
final Section section;
const SectionWidget({@required this.section});
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(section.title),
ListView.separated(
shrinkWrap: true,
scrollDirection: section.axis,
physics: section.axis == Axis.vertical
? NeverScrollableScrollPhysics()
: null,
itemCount: section.itemList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 100,
width: 100,
color: Colors.red,
); // Just to test if it works
},
separatorBuilder: (BuildContext context, int index) {
double paddingBetween = 10;
if (section.axis == Axis.horizontal) {
return SizedBox(width: paddingBetween);
} else {
return SizedBox(height: paddingBetween);
}
},
),
],
);
}
}