在Flutter中创建一个带圆角的按钮/带边框半径的按钮
更新
由于左侧的按钮已被弃用,请使用右侧的按钮。
Deprecated --> Recommended RaisedButton --> ElevatedButton OutlineButton --> OutlinedButton FlatButton --> TextButton
-
ElevatedButton
-
使用弧形边框
StadiumBorder
ElevatedButton( onPressed: () {}, child: Text('Button'), style: ElevatedButton.styleFrom(shape: StadiumBorder()), )
-
使用圆角矩形边框
RoundedRectangleBorder
ElevatedButton( onPressed: () {}, child: Text('Button'), style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), // <-- Radius ), ), )
-
使用圆形边框
CircleBorder
ElevatedButton( onPressed: () {}, child: Text('Button'), style: ElevatedButton.styleFrom( shape: CircleBorder(), padding: EdgeInsets.all(24), ), )
-
使用倾斜矩形边框
BeveledRectangleBorder
ElevatedButton( onPressed: () {}, child: Text('Button'), style: ElevatedButton.styleFrom( shape: BeveledRectangleBorder( borderRadius: BorderRadius.circular(12) ), ), )
OutlinedButton
-
使用弧形边框
StadiumBorder
OutlinedButton( onPressed: () {}, child: Text('Button'), style: OutlinedButton.styleFrom( shape: StadiumBorder(), ), )
-
使用圆角矩形边框
RoundedRectangleBorder
OutlinedButton( onPressed: () {}, child: Text('Button'), style: OutlinedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), )
-
使用圆形边框
CircleBorder
OutlinedButton( onPressed: () {}, child: Text('Button'), style: OutlinedButton.styleFrom( shape: CircleBorder(), padding: EdgeInsets.all(24), ), )
-
使用倾斜矩形边框
BeveledRectangleBorder
OutlinedButton( onPressed: () {}, child: Text('Button'), style: OutlinedButton.styleFrom( shape: BeveledRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), )
TextButton
TextButton
使用方式类似于 ElevatedButton
和 OutlinedButton
,不同之处在于只有在按钮被按下时才能看到形状。
1. 解决方案概述
FlatButton
和RaisedButton
已过时。
因此,你可以使用TextButton
和ElevatedButton
的style
属性中的shape
。
自Flutter 2.0以来,有一些变化:
style
:属性类型已更改为ButtonStyle
shape
:属性类型已更改为MaterialStateProperty
2. 圆角按钮
在style
属性中存在shape
属性:
style: ButtonStyle( shape: MaterialStateProperty.all( RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), side: BorderSide(color: Colors.red) ) ) )
方形按钮
对于一个方形的按钮,你可以使用ElevatedButton
或者添加以下代码:
style: ButtonStyle( shape: MaterialStateProperty.all( RoundedRectangleBorder( borderRadius: BorderRadius.zero, side: BorderSide(color: Colors.red) ) ) )
完整示例
Row( mainAxisAlignment: MainAxisAlignment.end, children: [ TextButton( child: Text( "Add to cart".toUpperCase(), style: TextStyle(fontSize: 14) ), style: ButtonStyle( padding: MaterialStateProperty.all(EdgeInsets.all(15)), foregroundColor: MaterialStateProperty.all (Colors.red), shape: MaterialStateProperty.all ( RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), side: BorderSide(color: Colors.red) ) ) ), onPressed: () => null ), SizedBox(width: 10), ElevatedButton( child: Text( "Buy now".toUpperCase(), style: TextStyle(fontSize: 14) ), style: ButtonStyle( foregroundColor: MaterialStateProperty.all (Colors.white), backgroundColor: MaterialStateProperty.all (Colors.red), shape: MaterialStateProperty.all ( RoundedRectangleBorder( borderRadius: BorderRadius.zero, side: BorderSide(color: Colors.red) ) ) ), onPressed: () => null ) ] )