将RaisedButton替换为ElevatedButton后按钮样式报错求助
修正后的完整代码
class _CalculatorState extends State<Calculator> { Widget calcbutton(String btntxt, Color btncolor, Color txtcolor) { return Container( child: ElevatedButton( onPressed: () { calculation(btntxt); }, child: Text( btntxt, style: TextStyle( fontSize: 35, color: txtcolor, ), ), style: ElevatedButton.styleFrom( backgroundColor: btncolor, padding: EdgeInsets.all(20), shape: CircleBorder(), ), ), ); } }
报错原因与解决说明
ElevatedButton是Flutter 2.0后替代RaisedButton的组件,它的样式系统做了重构,不再支持直接在组件根层级配置shape、padding、背景色这些属性,需要统一通过style参数管理:
- 背景色(原Color属性):RaisedButton的
Color参数对应ElevatedButton的backgroundColor,必须放在ElevatedButton.styleFrom()方法内配置 - shape属性:不再作为组件直接参数,需要移到
styleFrom的shape字段中 - padding属性:同样需要放到
styleFrom方法里,作为内部参数配置
内容的提问来源于stack exchange,提问作者acevol
相关产品推荐
相关产品推荐

