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

Flutter中ElevatedButton报错:命名参数'color'未定义

问题解决:ElevatedButton的color参数未定义错误

问题根源在于Flutter的API版本变更——新版本Flutter中,ElevatedButton.styleFrom方法不再使用color参数设置按钮背景色,取而代之的是backgroundColor参数。你当前代码里的style: ElevatedButton.styleFrom(color: Colors.blue)是旧版API写法,在新版环境下会触发参数未定义错误。

修正后的代码如下:

Center( 
  child: ElevatedButton(
    onPressed: () {
      // add your redirect functionality here
    },                
    child: const Text(
      'Button with redirect functionality',
      style: TextStyle(
        color: Colors.white,
      ),
    ),
    style: ElevatedButton.styleFrom(
      backgroundColor: Colors.blue, // 将color替换为backgroundColor
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(32.0),
      ),
    ),
  ),
),

如果你的项目依赖较旧版本的Flutter(SDK版本低于2.10.0左右),color参数原本是有效的,但建议升级依赖并使用新API,避免后续版本兼容问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 09:57:00