我目前正在Flutter开发一款Android应用.如何添加圆形按钮,类似于此?http://pertamini.co/rounded-button/
dhu*_*981 169
您可以使用RaisedButton小部件.Raised Button Widget具有shape属性,您可以使用它,如下面的代码段所示.
RaisedButton(
child: Text("Press Me"),
onPressed: null,
shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
)
Run Code Online (Sandbox Code Playgroud)
use*_*613 54
自 2020 年 9 月以来,Flutter 1.22.0:
“RaisedButton”和“FlatButton”均已弃用。
ElevatedButton.:代码:
ElevatedButton(
child: Text("ElevatedButton"),
onPressed: () => print("it's pressed"),
style: ElevatedButton.styleFrom(
primary: Colors.red,
onPrimary: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(32.0),
),
),
)
Run Code Online (Sandbox Code Playgroud)
不要忘记,还有一个.icon构造函数可以轻松添加图标:
ElevatedButton.icon(
icon: Icon(Icons.thumb_up),
label: Text("Like"),
onPressed: () => print("it's pressed"),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(32.0),
),
),
)
Run Code Online (Sandbox Code Playgroud)
OutlinedButton.:代码:
OutlinedButton.icon(
icon: Icon(Icons.star_outline),
label: Text("OutlinedButton"),
onPressed: () => print("it's pressed"),
style: ElevatedButton.styleFrom(
side: BorderSide(width: 2.0, color: Colors.blue),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(32.0),
),
),
)
Run Code Online (Sandbox Code Playgroud)
TextButton.:TextButton如果您不想要轮廓或颜色填充,您可以随时使用。
Abr*_*yev 33
您可以将形状用于FlatButton和RaisedButton。
对于圆形按钮:
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)
),
Run Code Online (Sandbox Code Playgroud)
对于方形按钮:
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(0.0),
side: BorderSide(color: Colors.red)
),
Run Code Online (Sandbox Code Playgroud)
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: Colors.white,
textColor: Colors.red,
padding: EdgeInsets.all(8.0),
onPressed: () {},
child: Text(
"Add to Cart".toUpperCase(),
style: TextStyle(
fontSize: 14.0,
),
),
),
SizedBox(width: 10),
RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
onPressed: () {},
color: Colors.red,
textColor: Colors.white,
child: Text("Buy now".toUpperCase(),
style: TextStyle(fontSize: 14)),
),
],
)
Run Code Online (Sandbox Code Playgroud)
Rah*_*kla 26
您可以简单地使用RaisedButton
Padding(
padding: EdgeInsets.only(left: 150.0, right: 0.0),
child: RaisedButton(
textColor: Colors.white,
color: Colors.black,
child: Text("Search"),
onPressed: () {},
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
),
)
Run Code Online (Sandbox Code Playgroud)
输出:
Bla*_*nka 25
你可以简单地使用RaisedButton,也可以使用InkWell获得自定义按钮和性质也一样onDoubleTap,onLongPress并且etc:
new InkWell(
onTap: () => print('hello'),
child: new Container(
//width: 100.0,
height: 50.0,
decoration: new BoxDecoration(
color: Colors.blueAccent,
border: new Border.all(color: Colors.white, width: 2.0),
borderRadius: new BorderRadius.circular(10.0),
),
child: new Center(child: new Text('Click Me', style: new TextStyle(fontSize: 18.0, color: Colors.white),),),
),
),
Run Code Online (Sandbox Code Playgroud)
如果你想使用splashColor,highlightColor在属性InkWell窗口小部件,使用Material小部件的父InkWell窗口小部件,而不是装饰容器(删除decoration属性).了解原因?在这里.
Cop*_*oad 18
有很多方法可以做到这一点。我在这里列出一些。
(1)使用 RoundedRectangleBorder
RaisedButton(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
onPressed: () {},
child: Text("Button"),
)
Run Code Online (Sandbox Code Playgroud)
(2)使用 ClipRRect
ClipRRect(
borderRadius: BorderRadius.circular(40),
child: RaisedButton(
onPressed: () {},
child: Text("Button"),
),
)
Run Code Online (Sandbox Code Playgroud)
(3)使用 ClipOval
ClipOval(
child: RaisedButton(
onPressed: () {},
child: Text("Button"),
),
)
Run Code Online (Sandbox Code Playgroud)
(4)使用 ButtonTheme
ButtonTheme(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: RaisedButton(
onPressed: () {},
child: Text("Button"),
),
)
Run Code Online (Sandbox Code Playgroud)
(5)使用 StadiumBorder
RaisedButton(
shape: StadiumBorder(),
onPressed: () {},
child: Text("Button"),
)
Run Code Online (Sandbox Code Playgroud)
kri*_*yaa 16
在新的更新中flutter 3.0flutter 使用Material 3指南
据此,按钮的默认边框是圆角的
Default Button
ElevatedButton(
onPressed: () {}, child: const Text("Default Button ")),
Run Code Online (Sandbox Code Playgroud)
Button with Border Radius Zero
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.zero)),
onPressed: () {},
child: const Text("Border Radius Zero ")),
Run Code Online (Sandbox Code Playgroud)
Button with custom border radius
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50))),
onPressed: () {},
child: const Text("Border Radius Custom ")),
Run Code Online (Sandbox Code Playgroud)
FilledButton注意:您可以对、TextButton等使用相同的逻辑。
有关按钮样式,请参阅https://m3.material.io/components/all-buttons 。
yob*_*rle 10
请改用TextButton。
FlatButton、RaishedButton 和 OutlineButton 等按钮据称自 2020 年 10 月起已被弃用。这是 Flutter 开发团队为简化和保持 Flutter API 一致性所做的努力之一,您可以使用 style 属性自定义其样式。
TextButton(
child: Padding(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
child: Text('Text here',
style: TextStyle(
color: Colors.teal,
fontSize: 14,
fontWeight: FontWeight.w500)),
),
style: TextButton.styleFrom(
primary: Colors.teal,
onSurface: Colors.yellow,
side: BorderSide(color: Colors.teal, width: 2),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(25))),
),
onPressed: () {
print('Pressed');
},
),
Run Code Online (Sandbox Code Playgroud)
小智 8
你可以使用这个代码:
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(borderRadius))),
),
child: Text("ok"),
)
Run Code Online (Sandbox Code Playgroud)
您可以使用以下代码制作带有渐变颜色的圆形按钮。
Container(
width: 130.0,
height: 43.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
gradient: LinearGradient(
// Where the linear gradient begins and ends
begin: Alignment.topRight,
end: Alignment.bottomLeft,
// Add one stop for each color. Stops should increase from 0 to 1
stops: [0.1, 0.9],
colors: [
// Colors are easy thanks to Flutter's Colors class.
Color(0xff1d83ab),
Color(0xff0cbab8),
],
),
),
child: FlatButton(
child: Text(
'Sign In',
style: TextStyle(
fontSize: 16.0,
fontFamily: 'Righteous',
fontWeight: FontWeight.w600,
),
),
textColor: Colors.white,
color: Colors.transparent,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
onPressed: () {
},
),
);
Run Code Online (Sandbox Code Playgroud)
小智 6
如果有人正在寻找完整的圆形按钮,那么我是这样实现的:
Center(
child: SizedBox.fromSize(
size: Size(80, 80), // Button width and height
child: ClipOval(
child: Material(
color: Colors.pink[300], // Button color
child: InkWell(
splashColor: Colors.yellow, // splash color
onTap: () {}, // Button pressed
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.linked_camera), // Icon
Text("Picture"), // Text
],
),
),
),
),
),
)
Run Code Online (Sandbox Code Playgroud)
要在button 中使用任何形状,请确保执行Button小部件中的所有代码:
**shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red) ),**
Run Code Online (Sandbox Code Playgroud)
如果你想让它变成正方形,使用BorderRadius.circular(0.0)它自动使它变成一个正方形。
按钮是这样的:
这是给定 UI 屏幕的所有源代码:
Scaffold(
backgroundColor: Color(0xFF8E44AD),
body: new Center(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(90, 10, 20, 0),
padding: new EdgeInsets.only(top: 92.0),
child: Text(
"Currency Converter",
style: TextStyle(
fontSize: 48,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
Container(
margin: EdgeInsets.only(),
padding: EdgeInsets.all(25),
child: TextFormField(
decoration: new InputDecoration(
filled: true,
fillColor: Colors.white,
labelText: "Amount",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
Container(
padding: EdgeInsets.all(25),
child: TextFormField(
decoration: new InputDecoration(
filled: true,
fillColor: Colors.white,
labelText: "From",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
Container(
padding: EdgeInsets.all(25),
child: TextFormField(
decoration: new InputDecoration(
filled: true,
fillColor: Colors.white,
labelText: "To",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)),
),
),
SizedBox(height: 20.0),
MaterialButton(
height: 58,
minWidth: 340,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(12)),
onPressed: () {},
child: Text(
"CONVERT",
style: TextStyle(
fontSize: 24,
color: Colors.black,
),
),
color: Color(0xFFF7CA18),
),
],
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
创建圆形按钮的不同方法如下:
带有 ElevatedButton.styleFrom 的 ElevatedButton
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
),
onPressed: () {},
child:
Text("Buy now".toUpperCase(), style: TextStyle(fontSize: 14)),
),
Run Code Online (Sandbox Code Playgroud)
带有 ButtonStyle 的 ElevatedButton
ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0),
))),
onPressed: () {},
child: Text("Submit".toUpperCase()),
),
Run Code Online (Sandbox Code Playgroud)
圆形按钮的实际演示可以在下面的 Dartpad 链接中找到:
您可以通过将透明颜色传递给内部的 color 属性来将此代码用于透明的圆形按钮BoxDecoration。例如。color: Colors.transparent. 另外,请注意此按钮仅使用Container和GestureDetector小部件。
Container(
height: 50.0,
child: GestureDetector(
onTap: () {},
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Color(0xFFF05A22),
style: BorderStyle.solid,
width: 1.0,
),
color: Colors.transparent,
borderRadius: BorderRadius.circular(30.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text(
"BUTTON",
style: TextStyle(
color: Color(0xFFF05A22),
fontFamily: 'Montserrat',
fontSize: 16,
fontWeight: FontWeight.w600,
letterSpacing: 1,
),
),
)
],
),
),
),
)
Run Code Online (Sandbox Code Playgroud)
这是您的问题的代码。您只需在 boxdecoration 中采用一个带有边框半径的简单容器即可。
new Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(15.0)),
color: Colors.blue,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: new Text(
"Next",
style: new TextStyle(
fontWeight: FontWeight.w500,
color: Colors.white,
fontSize: 15.0,
),
),
),
],
),
),
Run Code Online (Sandbox Code Playgroud)
RaisedButton(
child: Text("Button"),
onPressed: (){},
shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0),
side: BorderSide(color: Colors.red))
)
Run Code Online (Sandbox Code Playgroud)
您还可以使用ButtonTheme():
这是示例代码 -
ButtonTheme(
minWidth: 200.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.green)),
child: RaisedButton(
elevation: 5.0,
hoverColor: Colors.green,
color: Colors.amber,
child: Text(
"Place Order",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
onPressed: () {},
),
),
Run Code Online (Sandbox Code Playgroud)
创建圆形按钮的最简单方法之一是使用 a FlatButton,然后通过设置其shape属性来指定圆度。请按照下面的代码操作
FlatButton(
padding: EdgeInsets.all(30.0),
color: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
child: child: Text(
"Button",
style: TextStyle(color: Colors.white),
),
onPressed: () {
print('Button pressed');
},
),Run Code Online (Sandbox Code Playgroud)
注意:为了改变圆度,调整里面的值
BorderRadius.circular()
您还可以通过使用StadiumBorder形状来实现它:
FlatButton(
onPressed: () {},
child: Text('StadiumBorder'),
shape: StadiumBorder(),
color: Colors.pink,
textColor: Colors.white,
),
Run Code Online (Sandbox Code Playgroud)
如果你想使用的MaterialButton话,
您可以像这样添加给RoundedRectangleBorder定Shape,
MaterialButton(
onPressed: () {},
minWidth: MediaQuery.of(context).size.width * 0.4,
height: 34,
color: colorWhite,
highlightColor: colorSplash,
splashColor: colorSplash,
visualDensity: VisualDensity.compact,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
side: BorderSide(
color: colorGrey,
width: 0.6,
),
),
child: Text("CANCEL"),
),
Run Code Online (Sandbox Code Playgroud)
在 Null 安全性之后,使用 ElevatedButton 而不是 RaisingButton,因为 RaisingButton 已按文档所述折旧。
child: ElevatedButton(
onPressed: () {},
child: const Text('Add item to the list'),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Common.buttonColor),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74793 次 |
| 最近记录: |