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

Flutter抽屉开关时出现RenderFlex溢出及Leading部件宽度异常问题

Flutter抽屉展开/收起时的两个异常修复方案

异常1:RenderFlex右侧溢出(A RenderFlex overflowed by 128 pixels on the right)

问题根源在底部包含Logout按钮和版本号的Row组件,抽屉动画收缩时,按钮没有足够空间自适应,导致布局溢出。

修复步骤:

  1. 用Flexible包裹ElevatedButton.icon,让按钮在空间不足时自动压缩
  2. 适当缩小按钮的水平内边距,增加按钮与版本号的间距
  3. 把抽屉关闭时的宽度从0改为极小值4,避免动画过程中完全挤压内部组件

修改后的底部Row代码:

Visibility(
  visible: value.isOpen,
  child: Padding(
    padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Flexible(
          child: ElevatedButton.icon(
            onPressed: () {
              // TODO: Add logout logic here
            },
            icon: const Icon(Icons.logout, color: Colors.white),
            label: const Text(
              'Logout',
              style: TextStyle(color: Colors.white),
              overflow: TextOverflow.ellipsis,
            ),
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.blue,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10),
              ),
              padding: const EdgeInsets.symmetric(
                horizontal: 12, // 缩小水平内边距
                vertical: 10,
              ),
            ),
          ),
        ),
        const SizedBox(width: 8), // 增加间距避免拥挤
        const Text(
          'v1.0.0',
          overflow: TextOverflow.ellipsis,
          style: TextStyle(color: Colors.grey, fontSize: 12),
        ),
      ],
    ),
  ),
)

抽屉宽度修改:

width: value.isOpen ? 255 : 4,

异常2:Leading部件占满ListTile宽度(Leading widget consumes the entire tile width)

问题出在ExpansionTile展开后的子级MyListTile,当设置horizontalPadding:34和horizontalTitleGap:0时,Leading组件的布局空间计算异常,导致占满整个tile宽度。

修复步骤:

  1. 给Leading组件包裹固定宽度的SizedBox,强制限制其尺寸
  2. 将子级MyListTile的horizontalTitleGap从0改为小值8,避免布局冲突

修改后的MyListTile leading部分:

leading: SizedBox(
  width: iconSize,
  child: icon == ''
      ? Icon(Icons.circle, size: 8)
      : Image.network(
          width: iconSize,
          height: iconSize,
          icon,
          color: selectedItem ? Colors.blue : Colors.black54,
        ),
),

ExpansionTile子项调用修改:

MyListTile(
  context,
  horizontalPadding: 34,
  horizontalTitleGap: 8, // 从0改为8
  iconSize: 10,
  icon: child['icon'],
  title: child['title'],
),

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 09:07:02