Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical viewport was given unbounded height

People also ask

How do you solve a vertical viewport given unbounded height?

To fix the vertical viewport was given unbounded height error, you can either set the shrinkWrap property of the ListView widget to true or wrap it inside the Expanded/SizedBox widget (with height property).

What is viewport flutter?

Viewport is the visual workhorse of the scrolling machinery. It displays a subset of its children according to its own dimensions and the given offset. As the offset varies, different children are visible through the viewport.


Adding this two lines

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

This generally happens when you try to use a ListView/GridView inside a Column, there are many ways of solving it, I am listing few here.

  1. Wrap ListView in Expanded

    Column(
      children: <Widget>[
        Expanded( // wrap in Expanded
          child: ListView(...),
        ),
      ],
    )
    
  2. Wrap ListView in SizedBox and give a bounded height

    Column(
      children: <Widget>[
        SizedBox(
          height: 400, // fixed height
          child: ListView(...),
        ),
      ],
    )
    
  3. Use shrinkWrap: true in ListView.

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

So, everyone posted answers but no one cared to explain why: I'll copy the documentation about 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.

So, taking the vocabulary of the docs, what's happening here is that our ListView is in a situation of unbounded constraints (in the direction that we are scrolling), thus the ListView will complain that:

... a vertical viewport was given an unlimited amount of vertical space in which to expand.

By simply setting shrinkWrap to true will make sure that it wraps its size defined by the contents. A sample code to illustrate:

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

That's what is going with your code, nonetheless, @Rémi suggestion is the best for this case, using Align instead of a Column.


put grid view inside Flexible or Expanded widget

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

This situation typically happens when a scrollable widget is nested inside another scrollable widget.

In my case i use GridView inside of Column and that error throws.

GridView widget has shrinkWrap property/named parameter, to set it true my problem is fixed.

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

Although shrinkWrap do the thing, but you can't scroll in ListView.

If you want scrolling functionality, you can add physics property:

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

This answer is based on the north of the @CopsOnRoad

This happens because the ListView/GridView widget has no parent to take its size , so its height is infinite and is the flutter does not know the height of the ListView and cannot render.

  1. Solution one: fixed height

    Wrap it up directly with the Container or SizedBox to write the dead height.

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

This is fine, but if the height needs to be adaptive, it will not work if you write it.

  1. Solution two: shrinkWrap: true

    Try shrinkWrap: true with physics: ScrollPhysics() for scrolling

    shrinkWrap: true is a bit like in android's wrap_content.

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

However, after compiling, you will find a long yellow warning at the bottom of the page.

So still can't

  1. Solution three: Expanded or Flexible

    Flexible Is a flexible layout in flutter, equivalent to LinearLayout in android.

    There is one in Flexible flex The attribute, equal to layout_weight, takes up all the remaining space.

    So, we can wrap the ListView with Flexible.

    Column(
      children: <Widget>[
        Expanded( 
          child: ListView(...),
        ),
      ],
    )
    

This answer based :Vertical viewport was given unbounded height.