如何在列表视图中显示列表视图

时间:2019-10-17 20:15:03

标签: flutter flutter-listview

当我尝试在列表视图中显示列表视图时出现问题。现在,它向我显示错误“为垂直视口赋予了无限的高度”。我尝试添加一个容器,并将高度设置为第二个listview,并且可以正常工作。但是我不想限制第二个列表视图的高度。

return new Scaffold(
    body: new Column(
        children: <Widget> [
          new Expanded(
            child: ListView(
              children: <Widget> [
                new Container(), // something else
                new ListView.builder(
                    physics: NeverScrollableScrollPhysics(),
                    itemBuilder: (context, index) => new MenuCard(menuStore[index]),
                    itemCount: menuStore.length,
                    padding: new EdgeInsets.symmetric(vertical: 16.0)
                )
              ]
            )

          )
        ]
    )
);

3 个答案:

答案 0 :(得分:0)

您可能希望将ListView属性shrinkWrap设置为true

  /// Whether the extent of the scroll view in the [scrollDirection] should be
  /// determined by the contents being viewed.
  ///
  /// If the scroll view does not shrink wrap, then the scroll view will expand
  /// to the maximum allowed size in the [scrollDirection]. If the scroll view
  /// has unbounded constraints in the [scrollDirection], then [shrinkWrap] must
  /// be true.

...

  /// Defaults to false.
  final bool shrinkWrap;

答案 1 :(得分:0)

为了修复代码中的“给定错误”,您需要在ListView.builderListView中使用rinkleWrap:true。

 return new Scaffold(
        body: new Column(
            children: <Widget> [
              new Expanded(
                child: ListView(
                  shrinkWrap:ture,
                  children: <Widget> [
                    new Container(), // something else
                    new ListView.builder(
                        shrinkWrap:ture,
                        physics: NeverScrollableScrollPhysics(),
                        itemBuilder: (context, index) => new MenuCard(menuStore[index]),
                        itemCount: menuStore.length,
                        padding: new EdgeInsets.symmetric(vertical: 16.0)
                    )
                  ]
                )

              )
            ]
        )
    );

答案 2 :(得分:0)

在此处尝试此答案,您可以使用 ListView 代替 GridView ,还可以从 GridView 中删除crossAxisCount属性。

https://stackoverflow.com/a/53852317/3811162