Я пытаюсь создать пользовательский интерфейс галереи наподобие Netflix с горизонтальными ListViews внутри Vertical ListView, но я продолжаю получать ошибки области просмотра и не могу их обойти.
Полный код.
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,
);
}
}
Если я изменю вложенные ListViews на строки, которые отображаются, но не прокручиваются.
С вложенными 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.