实现思路
- 改变背景颜色:通过
ElevatedButton
的style
属性中的ButtonStyle
来设置backgroundColor
。
- 改变文本颜色:同样在
ButtonStyle
中设置foregroundColor
。
- 改变按下时水波纹颜色:在
ButtonStyle
里设置overlayColor
。
- 设置按钮形状为圆形:利用
RoundedRectangleBorder
结合BorderRadius.circular
来实现,将BorderRadius
设置为按钮宽度和高度一半的值,这样在正方形按钮上就呈现圆形。
关键代码
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
foregroundColor: MaterialStateProperty.all(Colors.white),
overlayColor: MaterialStateProperty.all(Colors.grey),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0),
)
)
),
child: Text('圆形按钮'),
)