我们经常会使用到GridView进行布局类似于iOS的CollectionView,可以实现网格状的布局。
但是会经常遇到一个问题,如果想和Column进行嵌套布局的时候,需要和GridView上面布局UI的时候会报错。
══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 5969): The following assertion was thrown during performResize():
I/flutter ( 5969): Vertical viewport was given unbounded height.
I/flutter ( 5969): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 5969): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 5969): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 5969): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 5969): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 5969): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 5969): the height of the viewport to the sum of the heights of its children.
当出现这种情况,GridView必须设置一个固定高度,可以使用SingleChildScrollView将Column和GridView处于同一层级下,并且设置GridView属性
GridView.count(
shrinkWrap:true,
),
GridView.count(
physics: NeverScrollableScrollPhysics(), // 处理GridView中滑动父级SingleChildScrollView无法滑动
)
即可解决嵌套无法进行滑动和未设置高度报错问题。
网友评论