即"漂浮"按钮,它默认带有阴影和灰色背景。按下后,阴影会变大
ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all(RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)))),
onPressed: (() {}),
child: Text("ElevatedButton"),
)
ElevatedButton(
style: ButtonStyle(
side: MaterialStateProperty.all(
BorderSide(color: Colors.red,width: 4))),
onPressed: (() {}),
child: Text("ElevatedButton"),
)
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.yellow)),
onPressed: (() {}),
child: Text("ElevatedButton"),
)
ElevatedButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(Colors.yellow)),
onPressed: (() {}),
child: Text("ElevatedButton"),
)
ElevatedButton(
style: ButtonStyle(
fixedSize:MaterialStateProperty.all(Size(200,300))),
onPressed: (() {}),
child: Text("ElevatedButton"),
)
只指定宽
fixedSize:MaterialStateProperty.all(Size.fromWidth(200))),
只指定高
fixedSize:MaterialStateProperty.all(Size.fromHeight(300))),
minimumSize和maximumSize 显示按钮的最小或最大的尺寸,fixedSize任受它们的约束
ElevatedButton(
style: ButtonStyle(
fixedSize:MaterialStateProperty.all(Size.fromWidth(300))
,maximumSize: MaterialStateProperty.all(Size.fromWidth(100))),
onPressed: (() {}),
child: Text("ElevatedButton"),
)
最大宽度设置100,但实际宽度设置300,最终显示100的宽度
ElevatedButton(
style: ButtonStyle(
textStyle: MaterialStateProperty.all(TextStyle(fontSize: 30)),
),
onPressed: (() {}),
child: Text("ElevatedButton"),
)
设置按钮内间距
在上面设置字体大小为30的前提下
ElevatedButton(
style: ButtonStyle(
textStyle: MaterialStateProperty.all(TextStyle(fontSize: 30)),
padding: MaterialStateProperty.all(EdgeInsets.all(30)),
),
onPressed: (() {}),
child: Text("ElevatedButton"),
)
在指定宽度的前提下,设置按钮里面文字显示的位置
按钮里文字右边居中显示:
ElevatedButton(
style: ButtonStyle(
alignment:Alignment.centerRight,
fixedSize: MaterialStateProperty.all(Size.fromWidth(300))
),
onPressed: (() {}),
child: Text("Button"),
)
按钮里文字左边居中显示:
ElevatedButton(
style: ButtonStyle(
alignment:Alignment.centerLeft,
fixedSize: MaterialStateProperty.all(Size.fromWidth(300))
),
onPressed: (() {}),
child: Text("Button"),
)
Alignment属性
/// The top left corner.
static const Alignment topLeft = Alignment(-1.0, -1.0);
/// The center point along the top edge.
static const Alignment topCenter = Alignment(0.0, -1.0);
/// The top right corner.
static const Alignment topRight = Alignment(1.0, -1.0);
/// The center point along the left edge.
static const Alignment centerLeft = Alignment(-1.0, 0.0);
/// The center point, both horizontally and vertically.
static const Alignment center = Alignment(0.0, 0.0);
/// The center point along the right edge.
static const Alignment centerRight = Alignment(1.0, 0.0);
/// The bottom left corner.
static const Alignment bottomLeft = Alignment(-1.0, 1.0);
/// The center point along the bottom edge.
static const Alignment bottomCenter = Alignment(0.0, 1.0);
/// The bottom right corner.
static const Alignment bottomRight = Alignment(1.0, 1.0);
ElevatedButton(
style: ButtonStyle(
shadowColor:MaterialStateProperty.all(Colors.green)
),
onPressed: (() {}),
child: Text("Button"),
)
不是很明显
ElevatedButton(
style: ButtonStyle(
elevation : MaterialStateProperty.all(10),
shadowColor:MaterialStateProperty.all(Colors.red)
),
onPressed: (() {}),
child: Text("Button"),
)
现在明显多了
ElevatedButton(
style: ButtonStyle(
overlayColor: MaterialStateProperty.all(Colors.red),
),
onPressed: (() {}),
child: Text("Button"),
)
OutlinedButton(
onPressed: (() {}),
child: Text("OutlinedButton"),
)
TextButton(
onPressed: (() {}),
child: Text("TextButton"),
)
附上官网地址:ButtonStyle class - material library - Dart API