Flutter的Text控件显示纯英文时高度和汉字不一致的问题

调整前:

调整后:

解决方案:

strutStyle: StrutStyle(

        forceStrutHeight: true,

        leading: 0.5,

),

解决原理:

中文和英文字体默认的leading不一样。

StrutStyle的leading属性是指字体的倍数,比如:0.5就是0.5*字体的高度,而且这个高度要分成两半,上下各分一半。

参考:

https://juejin.cn/post/7034416015442345991

全部代码:

Widget _buildChosenTags() {
  List<String> tags = ['全是汉字', 'all english', '汉字 english','全是汉字', 'all english', '汉字 english'];
  return Container(
    padding:EdgeInsets.all(8),
    child: Wrap(
      runSpacing: 15,
      spacing: 10,
      children: tags.map((String title) {
        return Container(
          padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
          decoration: BoxDecoration(
            border: Border.all(color: Colors.blueAccent, width: 0.5),
            borderRadius: BorderRadius.all(Radius.circular(2)),
          ),
          child: Text(
            title,
            //解决全英文的标签和中文标签高度不一致的问题
            strutStyle: StrutStyle(
              //forceStrutHeight: true,
              //leading: 0.5,
            ),
            style: TextStyle(fontSize: 16, color: Colors.grey),
          ),
        );
      }).toList(),
    ),
  );
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值