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

Flutter修改TextStyle FontWeight后Text/TextSpan仍显示加粗问题

问题

我有一个默认加粗的headline2文本样式,现在想基于它创建一个取消加粗的样式。尝试用textTheme.headline2!.copyWith(fontWeight: FontWeight.normal);或者apply(fontWeightDelta: -3)修改后,新样式的FontWeight值显示为3,但把它用到Text或TextSpan上时,文本还是加粗的。

代码示例

Expanded(
  child: Padding(
    padding: const EdgeInsets.only(top: GlobalSpacing.s),
    child: ListView.separated(
      shrinkWrap: true,
      separatorBuilder: (context, index) =>
          const Divider(
            thickness: 1,
          ),
      itemCount: controller.currentStep + 3,
      itemBuilder: (context, index) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            (controller.currentStep + 3 - 1 != index) ?
            Padding(
              padding: const EdgeInsets.fromLTRB(GlobalSpacing.m, 0.0, GlobalSpacing.m, 0.0),
              child: Text.rich(
                TextSpan(
                  style: textTheme.headline2,
                  text: "Text to be bold",
                  children: <TextSpan>[
                    TextSpan(
                      style: textTheme.headline2!.copyWith(fontWeight: FontWeight.normal)),
                      text: "text to be normal",
                    ),
                  ],
                ),
              ),
              /*
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text("Bold", style: textTheme.headline2),
                  const Spacer(),
                  Text("Not bold", style: textTheme.headline2!.copyWith(fontWeight: FontWeight.normal)),
                ],
              ), */
            ) :
            createCurrentNodeWidget(textTheme),
          ],
        );
      },
    ),
  ),
),
解决方案
  • 修正语法错误:你代码里第二个TextSpan的写法有问题,style参数后面多了一个闭合括号,导致text没有被纳入这个TextSpan的配置中,样式自然不会生效。修正后的正确写法:

    TextSpan(
      style: textTheme.headline2!.copyWith(fontWeight: FontWeight.normal),
      text: "text to be normal",
    )
    
  • 确认字体权重支持:有些字体的FontWeight.normal(对应权重400)和加粗版本视觉差异很小,或者你的主题可能指定了仅支持加粗的字体包。可以尝试显式指定FontWeight.w400替代FontWeight.normal,验证效果。

  • 验证样式是否正确覆盖:可以打印新样式的权重值,确认修改是否生效:

    final normalStyle = textTheme.headline2!.copyWith(fontWeight: FontWeight.normal);
    print(normalStyle.fontWeight); // 正常应该输出FontWeight.w400
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 05:30:43