这是我的代码:

@override
Widget build(BuildContext context) {
  return new Material(
    color: Colors.deepPurpleAccent,
    child: new Column(
     mainAxisAlignment: MainAxisAlignment.center,
        children:<Widget>[new GridView.count(crossAxisCount: _column,children: new List.generate(_row*_column, (index) {
          return new Center(
              child: new CellWidget()
          );
        }),)]
    )
  );
}

例外情况如下:

I/flutter ( 9925): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 9925): The following assertion was thrown during performResize():
I/flutter ( 9925): Vertical viewport was given unbounded height.
I/flutter ( 9925): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 9925): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 9925): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 9925): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 9925): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 9925): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 9925): the height of the viewport to the sum of the heights of its children.
I/flutter ( 9925): 
I/flutter ( 9925): When the exception was thrown, this was the stack:
I/flutter ( 9925): #0      RenderViewport.performResize.<anonymous closure> (package:flutter/src/rendering/viewport.dart:827:15)
I/flutter ( 9925): #1      RenderViewport.performResize (package:flutter/src/rendering/viewport.dart:880:6)
I/flutter ( 9925): #2      RenderObject.layout (package:flutter/src/rendering/object.dart:1555:9)

当前回答

如果你在2021年仍然面临这个问题…这里是最好的和简单的解决方案

      ListView.builder(
         scrollDirection: Axis.vertical,
         shrinkWrap: true,
     //     itemExtent: 400, use this if you give hight of your items
        physics: ScrollPhysics(),)

有很多解决办法。但是他们有问题…就像我们使用ListView并使用它的属性shrinkWrap .然后你会注意到滚动在ListView上不起作用所以你必须使用物理:ScrollPhysics()

其他回答

我做了一个自定义函数来解决你的问题。 你可以直接在你的代码库中使用这个函数:

Widget horizontalListView(height, width, color, child, margin) {
return Column(
  children: [
    SizedBox(
      height: 200,
      child: ListView.builder(
        itemCount: 10,
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        itemBuilder: (context,index) {
          return Container(
            height: height,
            width: width,
            margin: EdgeInsets.all(margin),
            color: color,
            child: child
          );
        }
      ),
    ),
  ],
 );
})

附注:对不起,代码对齐不好。

我面临这个问题,因为我想在一个ListView中显示一个GridView,解决方案是通过添加以下内容,不仅在ListView中,而且在GridView中:

收缩包装:真 scrollDirection: Axis.vertical

有很多答案,但每一个都有自己的局限性。每个人都在说使用shrinkWrap:没错。是的,你应该使用它,但这些代码的效果是,当你滚动列表时,它会自动移动到顶部项目,这意味着滚动回顶部。 所以完美的解决方案是使用物理收缩包装,只有列表才能顺利滚动。

      shrinkWrap: true,          //
      physics: ScrollPhysics(),  // both line is mandatory

如果你想看Listview的其他例子。生成器与列作为子和列表可以垂直滚动,然后你可以看到这里的例子-在垂直列表中排列多个文本。

虽然shrinkWrap可以做到,但你不能在ListView中滚动。

如果你想要滚动功能,你可以添加物理属性:

ListView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
    physics: ScrollPhysics(),
...

如果你在2021年仍然面临这个问题…这里是最好的和简单的解决方案

      ListView.builder(
         scrollDirection: Axis.vertical,
         shrinkWrap: true,
     //     itemExtent: 400, use this if you give hight of your items
        physics: ScrollPhysics(),)

有很多解决办法。但是他们有问题…就像我们使用ListView并使用它的属性shrinkWrap .然后你会注意到滚动在ListView上不起作用所以你必须使用物理:ScrollPhysics()