Dart 颤振中嵌套的ListView会产生水平视口错误
我试图制作一个像Netflix这样的图库用户界面,在垂直的ListView中包含水平的ListView,但我不断遇到视口错误,无法回避 完整代码Dart 颤振中嵌套的ListView会产生水平视口错误,dart,nested,flutter,flutter-layout,Dart,Nested,Flutter,Flutter Layout,我试图制作一个像Netflix这样的图库用户界面,在垂直的ListView中包含水平的ListView,但我不断遇到视口错误,无法回避 完整代码 class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Live Tree', home: Scaffold( appBar:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Live Tree',
home: Scaffold(
appBar: AppBar(
title: Text("Netflux"),
),
body: HomePage(),
),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
Row getMediaForCategory(CategoryModel category) {
List<Column> mediaItems = [];
for (Media media in category.media) {
mediaItems.add(
Column(
mainAxisSize: MainAxisSize.min,
children: [
media.image,
Container(
color: Colors.black,
padding: EdgeInsets.only(top: 8, bottom: 8),
child: Text(media.title),
)
],
),
);
}
return Row(mainAxisSize: MainAxisSize.min, children: mediaItems);
}
List<ListView> getCategoryRows(List<CategoryModel> categoryModels) {
List<ListView> categoryRows = [];
for (CategoryModel category in categoryModels) {
categoryRows.add(
ListView(
scrollDirection: Axis.horizontal,
children: [getMediaForCategory(category)]),
);
}
return categoryRows;
}
Widget gallerySection = ListView(
children: getCategoryRows(mockCategoryDataSet),
);
return Scaffold(
body: gallerySection,
);
}
}
问题是水平列表视图没有高度,因此最好使用
SingleChildScrollView
和行
,以便内容可以暗示高度:
List<Widget> getCategoryRows(List<CategoryModel> categoryModels) {
List<Widget> categoryRows = [];
for (CategoryModel category in categoryModels) {
categoryRows.add(
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [getMediaForCategory(category)],
),
),
);
}
return categoryRows;
}
List getCategoryRows(List categoryModels){
列表类别Rows=[];
for(categoryModels中的CategoryModel类别){
categoryRows.add(
SingleChildScrollView(
滚动方向:轴水平,
孩子:排(
子项:[getMediaForCategory(category)],
),
),
);
}
返回类别;
}
问题在于,您的水平列表视图没有高度,因此最好使用SingleChildScrollView
和行
,以便内容可以暗示高度:
List<Widget> getCategoryRows(List<CategoryModel> categoryModels) {
List<Widget> categoryRows = [];
for (CategoryModel category in categoryModels) {
categoryRows.add(
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [getMediaForCategory(category)],
),
),
);
}
return categoryRows;
}
List getCategoryRows(List categoryModels){
列表类别Rows=[];
for(categoryModels中的CategoryModel类别){
categoryRows.add(
SingleChildScrollView(
滚动方向:轴水平,
孩子:排(
子项:[getMediaForCategory(category)],
),
),
);
}
返回类别;
}
非常感谢您!但是,是否有必要设置容器的高度以匹配我的图像?如果我把它设置为100的高度,它会把我的照片切成两半。我想要的是与androids包装内容相同的内容。@JoelBroström是的,我已经更新了我的答案,因为这可能是更好的方式。非常感谢!但是,是否有必要设置容器的高度以匹配我的图像?如果我把它设置为100的高度,它会把我的照片切成两半。我想要的是与androids包装内容相等的内容。@JoelBroström是的,我已经更新了我的答案,因为这可能是更好的方法。