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

Flutter中TextButton设置字体加粗报错,如何通过styleFrom实现?

解决TextButton设置文本加粗的参数错误问题

你遇到的错误是因为TextButton.styleFrom()方法并没有提供直接的fontWeight参数,所以添加这个参数会触发参数不匹配的报错。

要实现按钮文本加粗,有两种正确的方式:

方式一:直接在Text组件中设置

把字体加粗的样式直接写在按钮的Text子组件里,这是最直观的做法:

TextButton(
  onPressed: () {},
  child: Text(
    "Don't have account?",
    style: TextStyle(fontWeight: FontWeight.bold),
  ),
  style: TextButton.styleFrom(
    foregroundColor: Colors.black,
    padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
  ),
),

方式二:通过styleFrom的textStyle参数设置

如果一定要通过styleFrom()来统一控制文本样式,可以使用textStyle参数传入TextStyle对象:

TextButton(
  onPressed: () {},
  child: Text("Don't have account?"),
  style: TextButton.styleFrom(
    foregroundColor: Colors.black,
    textStyle: TextStyle(fontWeight: FontWeight.bold),
    padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
  ),
),

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 13:08:12