You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用SingleChildScrollView?代码报错问题修复咨询

代码修复方案

问题出在SingleChildScrollView的滚动方向配置上——它默认是垂直滚动模式,和内部水平布局的Row不匹配,导致子组件总宽度超出屏幕时触发溢出错误。

修复后的代码:

SingleChildScrollView(
  // 指定为水平滚动方向,适配Row的布局逻辑
  scrollDirection: Axis.horizontal,
  // 可选:添加左右内边距,避免内容贴紧屏幕边缘
  padding: const EdgeInsets.symmetric(horizontal: 16),
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceAround,
    children: [
      weightIndicator(0, '0,2 kg'),
      weightIndicator(0, '0,4 kg'),
      weightIndicator(0, '0,6 kg'),
      weightIndicator(0, '0,8 kg'),
      weightIndicator(0, '1,0 kg'),
    ],
  ),
)

额外提示:如果weightIndicator是你自定义的组件,要确保它的宽度为固定值或有合理的自适应逻辑,避免无限制拉伸导致滚动异常。

内容的提问来源于stack exchange,提问作者cindy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 21:11:01