【问题标题】:Nested ListViews in flutter gives horizontal viewport error颤动中的嵌套ListViews会产生水平视口错误
【发布时间】:2019-01-07 12:23:07
【问题描述】:

我正在尝试在垂直 ListView 中制作一个像 Netflix 一样的画廊 UI,其中包含水平 ListViews,但我不断收到视口错误并且无法绕过它。

完整代码。

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.

【问题讨论】:

    标签: dart nested flutter flutter-layout


    【解决方案1】:

    问题在于您的水平列表视图没有高度,因此您最好使用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;
    }
    

    【讨论】:

    • 非常感谢!但是,无论如何设置容器的高度以匹配我的图像?如果我将其设置为高度 100,它将把我的照片切成两半。我想要的是等同于 androids wrap 内容的东西。
    • @JoelBroström 是的,我已经更新了我的答案,因为无论如何这可能是更好的方法。
    猜你喜欢
    • 1970-01-01
    • 2022-11-25
    • 2021-02-06
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2023-03-10
    • 2021-01-03
    • 1970-01-01
    相关资源
    最近更新 更多