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

如何修改Flutter中ElevatedButton的背景颜色?

修改ElevatedButton背景颜色的方法

方法一:直接设置固定背景色

使用ElevatedButton.styleFrom()方法直接指定backgroundColor参数,是最直观的修改方式:

child: ElevatedButton(                  
  onPressed: (){},
  style: ElevatedButton.styleFrom(
    backgroundColor: Colors.blue, // 替换为你需要的颜色
  ),
  child: Text('Get Started', 
    style: TextStyle(
      color: Colors.white,
      fontSize: 20.0,
    ),
  ),
),

方法二:按按钮状态设置不同背景色

如果需要根据按钮的状态(如按下、禁用)显示不同背景色,可使用ButtonStyle配合MaterialStateProperty.resolveWith实现:

child: ElevatedButton(                  
  onPressed: (){},
  style: ButtonStyle(
    backgroundColor: MaterialStateProperty.resolveWith<Color>(
      (Set<MaterialState> states) {
        if (states.contains(MaterialState.pressed)) {
          return Colors.blueAccent; // 按下时的背景色
        }
        if (states.contains(MaterialState.disabled)) {
          return Colors.grey; // 禁用时的背景色
        }
        return Colors.blue; // 默认背景色
      },
    ),
  ),
  child: Text('Get Started', 
    style: TextStyle(
      color: Colors.white,
      fontSize: 20.0,
    ),
  ),
),

内容的提问来源于stack exchange,提问作者EL AYADI FATIMAEZZAHRA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 18:20:38