Vertical viewport was given unbounded height

后端 未结 10 1226
不思量自难忘°
不思量自难忘° 2020-11-30 22:30

This is my code:

  @override
  Widget build(BuildContext context) {
    return new Material(
      color: Colors.deepPurpleAccent,
      child: new Column(
           


        
10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 22:40

    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: [
          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: [
          Expanded( 
            child: ListView(...),
          ),
        ],
      )
      

    This answer based :Vertical viewport was given unbounded height.

提交回复
热议问题