统一 StrutStyle 强制恒定行高,解决数字+中文+英文居中问题:(优先用这个方法)

下面代码解释:行高用 StrutStyle 锁死(解决 DIN 数字 vs 中文字体混排基线问题),元素对齐交给 Row 默认 center,水平布局用 Expanded 占位两端撑开。forceStrutHeight: true 是这套方案的关键开关。

    final titleStrut = StrutStyle(
      fontSize: 15.sw,
      height: 23 / 15,
      forceStrutHeight: true,
    );

Row(
          children: [
            Image.asset(iconAsset, width: 16.sw, height: 16.sw),
            SizedBox(width: 8.w),
            Text(
              title,
              style: TextStyle(
                fontSize: 15.sw,
                fontWeight: FontWeight.w500,
                color: AppColor.textColor12,
                height: 23 / 15,
              ),
              strutStyle: titleStrut,
            ),
            SizedBox(width: 8.w),
            Text(
              marketValue?.priceFixed() ?? '--',
              style: WBTextStyle_DIN(
                fontSize: 15.sw,
                fontWeight: FontWeight.w500,
                color: AppColor.textColor12,
                height: 23 / 15,
              ),
              strutStyle: titleStrut,
            ),
            Text(
              currency,
              style: TextStyle(
                fontSize: 15.sw,
                fontWeight: FontWeight.w500,
                color: AppColor.textColor12,
                height: 23 / 15,
              ),
              strutStyle: titleStrut,
            ),
            const Expanded(child: SizedBox.shrink()),
          ],
        )

对底部参考链接文章分析如下:

文章的关键点:*TextStyle 的 height 控制的是行高倍数,但文本内容在行高内并不是居中的*。这是因为字体的 ascent 和 descent 不对称(大写字母没有下沉部分,但字体仍保留了 descender 空间)。                                                                                                               
文章提到的小技巧是:通过调整 TextStyle 的 height 来修正居中。当前 height: 1 让行高等于 fontSize,但文本在行高内偏上。

根据文章的分析,StrutStyle 的 forceStrutHeight: true 会覆盖 TextStyle 的 height,而且 StrutStyle 的 height 影响的是 ascent+descent 的量度,文本在这个量度内仍然不是居中的。 

文章给出的核心思路是:文本在行高内天然不居中(ascent > descent),需要微调 TextStyle 的 height 来补偿。

根据文章分析,英文字体的 ascent(上半部)比 descent(下半部)大,所以文本在容器中视觉偏上是字体本身的特性,height 和 StrutStyle 都无法完全修正。          

方案一:

去掉容器高度,通过设置上下padding微调实现

注:文本height一定要设置

Container(
      alignment: Alignment.center,
      padding: EdgeInsets.only(left: 1.53.w, right: 1.47.w, top: 1.7.sw, bottom: 0.3.sw),
      decoration: BoxDecoration(
        border: Border.all(
          color: bgColor,
          width: 1.w,
        ),
        borderRadius: BorderRadius.all(Radius.circular(1.sw)),
      ),
      child: Text(
        marketType,
        style: TextStyle(
          fontSize: 9.sw,
          color: bgColor,
          height: 1,
        ),
      ),
    );

                                           

方案二:(推荐)

是用 Transform.translate 做微小偏移补偿(Offset(0, 0.5)),如果还偏可以微调这个值。这样不影响布局计算,只做视觉补偿。代码如下

Container(
      alignment: Alignment.center,
      height: 11,
      padding: EdgeInsets.symmetric(horizontal: 1.53),
      decoration: BoxDecoration(
        border: Border.all(
          color: bgColor,
          width: 1,
        ),
        borderRadius: BorderRadius.all(Radius.circular(1)),
      ),
      child: Transform.translate(
        offset: Offset(0, 0.5),
        child: Text(
          marketType,
          style: TextStyle(
            fontSize: 9,
            color: bgColor,
            height: 1,
          ),
        ),
      ),
    );

思考:设置行高height后效果上看偏上,那为什么不给container设置顶部内边距

可以,但效果不完全等价:

  1. Transform.translate(Offset(0, 0.5)):纯视觉偏移,不影响布局计算,文本精确下移 0.5 
  2. 顶部内边距:改变内容区域大小,Alignment.center 会在缩小后的空间内重新居中,视觉下移量只有 padding 值的一半  

所以用 top padding 的话,要达到同样的 0.5 下移效果,需要设置 top padding。                             但有个副作用:顶部内边距会让容器实际留给文本的高度变小,对于这种小尺寸组件(11 高)可能显得拥挤。Transform.translate 不影响布局空间,视觉补偿更干净。                                         建议保留 Transform.translate 方案。   

参考:https://juejin.cn/post/7108463516952035365

更多推荐