垂直可视窗口高度未被限定。

28 浏览
0 Comments

垂直可视窗口高度未被限定。

这是我的代码:

@override
Widget build(BuildContext context) {
  return new Material(
    color: Colors.deepPurpleAccent,
    child: new Column(
     mainAxisAlignment: MainAxisAlignment.center,
        children:[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. (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)

admin 更改状态以发布 2023年5月20日
0
0 Comments

当你尝试把ListView / GridView放在Column中时,通常会发生这种情况,有许多解决方法,我在这里列出了几种。

  1. ListView 包装在 Expanded

    Column(
      children: [
        Expanded( // wrap in Expanded
          child: ListView(...),
        ),
      ],
    )
    

  2. ListView 包装在 SizedBox 中,并给予指定的 height

    Column(
      children: [
        SizedBox(
          height: 400, // fixed height
          child: ListView(...),
        ),
      ],
    )
    

  3. ListView 中使用 shrinkWrap: true

    Column(
      children: [
        ListView(
          shrinkWrap: true, // use this
        ),
      ],
    )
    

0
0 Comments

添加这两行

ListView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
...

0