Flutter饮品电商APP收藏页RenderFlex底部溢出125像素求助
收藏页RenderFlex底部溢出问题解决
问题描述
开发饮品配送Flutter电商APP时,收藏页出现RenderFlex底部溢出125像素报错,提示由Column组件导致,尝试使用Expanded、SizedBox组件未解决。
报错截图


问题代码
Container( height: 1500.0, width: double.infinity, child: Column( children: [ const Text( 'My Favourites', style: TextStyle(fontSize: 40.0), ), GridView.builder( shrinkWrap: true, itemCount: favItems.length, itemBuilder: ((context, index) { return ProductItem( id: favItems[index].id, bottleName: favItems[index].bottleName, imgUrl: favItems[index].image, price: favItems[index].price, bottle: favItems[index]); }), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, childAspectRatio: itemWidth / itemHeight, ), ), ], ), ));
解决方案
问题根源:外层Container固定了1500px高度,当GridView内收藏商品数量增多时,整体高度超过Container限制导致溢出;同时Column内的GridView未正确分配剩余空间。
方案一:自适应屏幕剩余空间(推荐)
- 移除Container的固定
height: 1500.0,让布局自适应屏幕尺寸 - 将GridView包裹在Expanded组件中,使其占据Column的剩余可用空间,同时去掉
shrinkWrap: true(Expanded已提供明确约束,shrinkWrap会引发高度计算冲突)
修改后的代码:
Container( width: double.infinity, child: Column( children: [ const Text( 'My Favourites', style: TextStyle(fontSize: 40.0), ), Expanded( child: GridView.builder( itemCount: favItems.length, itemBuilder: ((context, index) { return ProductItem( id: favItems[index].id, bottleName: favItems[index].bottleName, imgUrl: favItems[index].image, price: favItems[index].price, bottle: favItems[index]); }), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, childAspectRatio: itemWidth / itemHeight, ), ), ), ], ), ));
方案二:整页可滚动(适合内容过多场景)
如果需要标题随内容一起滚动,可改用SingleChildScrollView包裹Column,同时给GridView设置shrinkWrap: true和physics: NeverScrollableScrollPhysics():
SingleChildScrollView( child: Column( children: [ const Text( 'My Favourites', style: TextStyle(fontSize: 40.0), ), GridView.builder( shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemCount: favItems.length, itemBuilder: ((context, index) { return ProductItem( id: favItems[index].id, bottleName: favItems[index].bottleName, imgUrl: favItems[index].image, price: favItems[index].price, bottle: favItems[index]); }), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, childAspectRatio: itemWidth / itemHeight, ), ), ], ), ));
内容的提问来源于stack exchange,提问作者Anesu Mazvimavi
相关产品推荐
相关产品推荐

