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

Flutter中Row内Expanded文本无法实现省略效果问题

解决方案

问题核心是右侧内容区域没有被限制宽度,导致Text可以无限延伸,Expanded和省略配置无法生效。按以下步骤修改:

  1. 给包裹右侧Column的Padding组件外层添加Expanded,让它占据Row的剩余空间,限制内容宽度
  2. 移除包裹Text的多余Row(或保留Row但去掉mainAxisSize: MainAxisSize.min,因为该属性会让Row尽可能窄,Expanded无法生效)

修改后的完整代码:

SingleChildScrollView(
  child: Column(
    children: [
      Container(...),
      Column(
        children: List.generate(data.length, (index) {
          return Padding(
            padding: const EdgeInsets.only(top: 16.0),
            child: Container(
              color: Colors.white,
              constraints: const BoxConstraints(
                minHeight: 120,
              ),
              child: Row(children: [
                Container(
                  color: _switchColor(data[index].type),
                  width: 15,
                  height: 120,
                ),
                // 新增:用Expanded包裹右侧Padding,限制宽度
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 50.0, right: 14),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          data[index].title,
                          style: const TextStyle(
                            color: Colors.black,
                            fontWeight: FontWeight.w600,
                            fontSize: 16,
                          ),
                        ),
                        // 移除多余的Row,直接用Expanded包裹Text
                        Expanded(
                          child: Text(
                            data[index].description,
                            style: const TextStyle(color: Colors.black),
                            maxLines: 3,
                            overflow: TextOverflow.ellipsis,
                          ),
                        )
                      ],
                    ),
                  ),
                )
              ]),
            ),
          );
        }),
      ),
    ],
  ),
);

如果一定要保留包裹Text的Row,修改如下:

// 替换原有的Row部分
Row(
  // 去掉mainAxisSize: MainAxisSize.min,让Row占满父容器宽度
  children: [
    Expanded(
      child: Text(
        data[index].description,
        style: const TextStyle(color: Colors.black),
        maxLines: 3,
        overflow: TextOverflow.ellipsis,
      ),
    ),
  ],
)

关键逻辑说明

  • 外层Row中,左侧色块是固定宽度,右侧内容必须用Expanded约束,否则Column会随Text内容无限扩展,导致省略失效
  • MainAxisSize.min会让Row/Column尽可能收缩,此时内部的Expanded无法获取可用空间,因此必须移除该属性(或直接去掉多余的Row)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:13:16