今天做網易雲音樂某頁面使用GridView時,發生下列報錯信息:
flutter: The following assertion was thrown during performResize():
flutter: Vertical viewport was given unbounded height.
flutter: Viewports expand in the scrolling direction to fill their container.In this case, a vertical
flutter: viewport was given an unlimited amount of vertical space in which to expand. This situation
flutter: typically happens when a scrollable widget is nested inside another scrollable widget.
flutter: If this widget is always nested in a scrollable widget there is no need to use a viewport because
flutter: there will always be enough vertical space for the children. In this case, consider using a Column
flutter: instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
flutter: the height of the viewport to the sum of the heights of its children.
這是GridView部分代碼:
GridView(
padding: EdgeInsets.symmetric(vertical: 20.0),
children: songWidgets,
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 10.0,
crossAxisSpacing: 15.0,
childAspectRatio: 5/7
),
),
經過觀察發現,未添加shrinkWrap
屬性,該屬性表示是否根據子組件的總長度來設置GridView的長度,默認值爲false 。默認情況下,GridView的會在滾動方向儘可能多的佔用空間。當GridView在一個無邊界(滾動方向上)的容器中時,shrinkWrap必須爲true。因爲這裏的GridVIew禁止滾動了,所以需要將shrinkWrap
設置爲true
,或者在外面嵌套一層有尺寸的Container
也可以。最後放上效果圖: