美文网首页
Widget--按钮Button

Widget--按钮Button

作者: Liu_QT | 来源:发表于2021-04-28 16:13 被阅读0次

Material 组件库中提供了多种按钮组件如RaisedButton、FlatButton、OutlineButton等。
所有Material 库中的按钮都有如下相同点:

  1. 按下时都会有“水波动画”(就是点击时按钮上会出现水波荡漾的动画)。
  2. 有一个onPressed属性来设置点击回调,当按钮按下时会执行该回调,如果不提供该回调则按钮会处于禁用状态,禁用状态不响应用户点击。

RaisedButton

即"漂浮"按钮,它默认带有阴影和灰色背景。按下后,阴影会变大,如图所示:

image.png

使用RaisedButton非常简单,如:

RaisedButton(
  child: Text("normal"),
  onPressed: () {},
);

FlatButton

即扁平按钮,默认背景透明并不带阴影。按下后,会有背景色,如图所示:

image.png

使用FlatButton也很简单,代码如下:

FlatButton(
  child: Text("normal"),
  onPressed: () {},
)

OutlineButton

默认有一个边框,不带阴影且背景透明。按下后,边框颜色会变亮、同时出现背景和阴影(较弱),如图所示:

image.png

使用OutlineButton也很简单,代码如下:

OutlineButton(
  child: Text("normal"),
  onPressed: () {},
)

IconButton

是一个可点击的Icon,不包括文字,默认没有背景,点击后会出现背景,如图所示:

image.png

代码如下:

IconButton(
  icon: Icon(Icons.thumb_up),
  onPressed: () {},
)

带图标的按钮

RaisedButtonFlatButtonOutlineButton都有一个icon 构造函数,通过它可以轻松创建带图标的按钮,如图所示:

图3-14

代码如下:

RaisedButton.icon(
  icon: Icon(Icons.send),
  label: Text("发送"),
  onPressed: _onPressed,
),
OutlineButton.icon(
  icon: Icon(Icons.add),
  label: Text("添加"),
  onPressed: _onPressed,
),
FlatButton.icon(
  icon: Icon(Icons.info),
  label: Text("详情"),
  onPressed: _onPressed,
),

自定义按钮外观

按钮外观可以通过其属性来定义,不同按钮属性大同小异,我们以FlatButton为例,介绍一下常见的按钮属性。

const FlatButton({
  ...  
  @required this.onPressed, //按钮点击回调
  this.textColor, //按钮文字颜色
  this.disabledTextColor, //按钮禁用时的文字颜色
  this.color, //按钮背景颜色
  this.disabledColor,//按钮禁用时的背景颜色
  this.highlightColor, //按钮按下时的背景颜色
  this.splashColor, //点击时,水波动画中水波的颜色
  this.colorBrightness,//按钮主题,默认是浅色主题 
  this.padding, //按钮的填充
  this.shape, //外形
  @required this.child, //按钮的内容
})

定义一个背景蓝色,两边圆角的按钮。效果如图所示:

image.png

代码如下:

FlatButton(
  color: Colors.blue,
  highlightColor: Colors.blue[700],
  colorBrightness: Brightness.dark,
  splashColor: Colors.grey,
  child: Text("Submit"),
  shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
  onPressed: () {},
)

这个按钮没有阴影(点击之后也没有),这样会显得没有质感。其实这也很容易,将上面的FlatButton换成RaisedButton就行,其它代码不用改(这里 color 也不做更改),换了之后的效果如图所示:

image.png

因为RaisedButton默认有配置阴影:

const RaisedButton({
  ...
  this.elevation = 2.0, //正常状态下的阴影
  this.highlightElevation = 8.0,//按下时的阴影
  this.disabledElevation = 0.0,// 禁用时的阴影
  ...
}

在Material 组件库中,我们会在很多组件中见到elevation相关的属性,它们都是用来控制阴影的,这是因为阴影在Material设计风格中是一种很重要的表现形式。

相关文章

网友评论

      本文标题:Widget--按钮Button

      本文链接:https://www.haomeiwen.com/subject/pldprltx.html