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

如何在Flutter中用一行代码修改TextButton的背景色与文字颜色?

在Flutter中一行代码修改TextButton的背景色与文字颜色

要快速调整TextButton的样式,官方推荐使用TextButton.styleFrom()方法,这是最简洁的配置方式:

仅修改背景色

在style参数中传入配置了backgroundColor的styleFrom()即可:

TextButton(
  style: TextButton.styleFrom(backgroundColor: Colors.red),
  onPressed: () {}, // 必填的点击回调
  child: const Text("示例按钮"),
),

同时修改文字颜色与背景色

在styleFrom()中同时指定textColor和backgroundColor参数:

TextButton(
  style: TextButton.styleFrom(
    textColor: Colors.white,
    backgroundColor: Colors.red,
  ),
  onPressed: () {},
  child: const Text("示例按钮"),
),

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 04:20:27