我們經(jīng)常會使用到GridView進行布局類似于iOS的CollectionView,可以實現(xiàn)網(wǎng)格狀的布局。
但是會經(jīng)常遇到一個問題内舟,如果想和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.
當出現(xiàn)這種情況验游,GridView必須設(shè)置一個固定高度,可以使用SingleChildScrollView將Column和GridView處于同一層級下,并且設(shè)置GridView屬性
GridView.count(
??shrinkWrap:true,
),
GridView.count(
??physics: NeverScrollableScrollPhysics(),?// 處理GridView中滑動父級SingleChildScrollView無法滑動
)
即可解決嵌套無法進行滑動和未設(shè)置高度報錯問題。