Flutter中已弃用的RaisedButton如何替换为ElevatedButton实现相同效果
等效实现代码
ElevatedButton( onPressed: null, style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(24.0), ), padding: EdgeInsets.all(12.0), backgroundColor: Colors.blue, foregroundColor: Colors.white, ), child: Text("Button"), )
配置说明
- ElevatedButton的所有样式属性统一通过
style参数配置,使用官方提供的ElevatedButton.styleFrom静态方法可以快速完成常用样式设置,不需要自定义Style类。 - 原RaisedButton的
shape、padding属性直接迁移到styleFrom方法内即可。 - 原
color属性对应styleFrom内的backgroundColor参数,原Text组件中设置的白色文字样式,可以直接通过foregroundColor参数统一配置,无需单独给Text组件设置样式,效果完全一致。 onPressed参数逻辑和旧版RaisedButton完全一致,传null时按钮为禁用状态,表现无差异。
内容的提问来源于stack exchange,提问作者user1209216
相关产品推荐
相关产品推荐

