这是我的代码:

@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)

当前回答

在我的情况下,我必须包装ListView在一个容器内,并给这个容器一个特定的高度

其他回答

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

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

ListView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
    physics: 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.builder ( scrollDirection: Axis.vertical, 收缩包装:没错,

...

这种情况通常发生在可滚动小部件嵌套在另一个可滚动小部件中时。

在我的情况下,我使用GridView在列和错误抛出。

GridView小部件有shrinkWrap属性/命名参数,将其设置为真,我的问题已经解决。

GridView.count(
  shrinkWrap: true,
  // rest of code
)

所以,每个人都上传了答案,但没有人愿意解释原因: 我将复制关于shrinkWrap的文档:

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. Shrink wrapping the content of the scroll view is significantly more expensive than expanding to the maximum allowed size because the content can expand and contract during scrolling, which means the size of the scroll view needs to be recomputed whenever the scroll position changes. Defaults to false.

所以,从文档的词汇表来看,这里发生的是我们的ListView处于一个无限约束的情况(在我们滚动的方向上),因此ListView会抱怨:

... 垂直视口被赋予无限的垂直空间 它需要扩展。

通过简单地将shrinkWrap设置为true,将确保它包装由内容定义的大小。示例代码说明:

// ...
  ListView(
    // Says to the `ListView` that it must wrap its
    // height (if it's vertical) and width (if it's horizontal).
    shrinkWrap: true,
  ),
// ...

这就是你的代码,尽管如此,@Rémi建议是最好的,使用Align而不是Column。