gpt4 book ai didi

dart - flutter 中的嵌套 ListViews 给出水平视口(viewport)错误

转载 作者:IT王子 更新时间:2023-10-29 07:23:49 25 4
gpt4 key购买 nike

我试图制作像 Netflix 这样的图库 UI,在垂直 ListView 中使用水平 ListView,但我一直收到视口(viewport)错误并且无法解决。

完整代码。

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,
);
}
}

如果我将嵌套的 ListView 更改为行,则会呈现但不可滚动。

使用嵌套的 ListViews 我得到以下错误:

I/flutter ( 9048): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 9048): The following assertion was thrown during performResize():
I/flutter ( 9048): Horizontal viewport was given unbounded height.
I/flutter ( 9048): Viewports expand in the cross axis to fill their container and constrain their children to match
I/flutter ( 9048): their extent in the cross axis. In this case, a horizontal viewport was given an unlimited amount of
I/flutter ( 9048): vertical space in which to expand.

最佳答案

问题是你的水平 ListView 没有高度所以你最好使用 SingleChildScrollViewRow 这样高度可以通过内容:

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;
}

关于dart - flutter 中的嵌套 ListViews 给出水平视口(viewport)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com