Flutter中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
相关产品推荐
相关产品推荐

