使用SingleChildScrollView实现滚动页面报RenderFlex高度无约束错误如何解决
报错根因
RenderFlex children have non-zero flex but incoming height constraints are unbounded 报错的核心冲突点:
SingleChildScrollView本身不会给子组件施加垂直高度上限,内部布局的高度是完全无界的- 你在
SingleChildScrollView内嵌的Column中,使用了Expanded组件包裹横向滚动的ScrollSnapList,Expanded的逻辑是占用父Column的剩余可用高度,但父级处于无界高度环境下,根本无法计算剩余高度,因此触发约束异常
修复方案
直接删除包裹ScrollSnapList的Expanded组件,改为给ScrollSnapList指定固定高度即可,你的天气卡片已经预设了固定高度,匹配对应高度即可:
// 替换原有 Expanded 包裹的 ScrollSnapList 代码段 SizedBox( // 你的单个天气卡片高度为200,加上上下40的margin总高280,可自行调整到合适数值 height: 280, child: ScrollSnapList( shrinkWrap: true, onItemFocus: _onItemFocus, itemSize: 150, scrollDirection: Axis.horizontal, itemCount: weatherData.length, itemBuilder: (context, index) { // 原有itemBuilder逻辑完全不变 double scale = 1.0; if (topContainer > 0.5) { scale = index + 0.5 - topContainer; if (scale < 0) { scale = 0; } else if (scale > 1) { scale = 1; } } return GestureDetector( child: Opacity( opacity: scale, child: Transform( transform: Matrix4.identity()..scale(scale, scale), alignment: Alignment.bottomCenter, child: Align( heightFactor: 1.0, alignment: Alignment.topCenter, child: weatherData[index]), ), ), ); }), )
补充说明
你代码中给横向滚动的ScrollSnapList设置的shrinkWrap: true是针对垂直滚动场景的属性,此处不影响功能,保留或删除都可。
内容的提问来源于stack exchange,提问作者KYflutterrookie
相关产品推荐
相关产品推荐

