
2.7 按钮Button
在Flutter中,Button常用于处理用户的点击事件操作,与Android中的Button和iOS中的UIButton功能类似。Flutter提供了很多预定样式的Button,表2-10列出了一些常用的Button。
表2-10 Flutter中常用的Button类别及说明

MaterialButton是Material Design风格按钮,需要在MaterialApp组件中使用,它常用的按钮子类有RaisedButton、FlatButton、OutlineButton。基本使用如下。


2-25 Material-Button按钮的基本使用效果图
MaterialButton的构造函数说明代码如下。

RaisedButton默认有阴影高度、背景填充,如图2-20所示。它的基本使用代码如下。


图2-20 RaisedButton的基本使用效果图
FlatButton默认无背景填充,也无阴影高度,如图2-21所示。

图2-21 FlatButton的基本使用效果图

OutlineButton默认情况下会带有边框,当点击时,会有填充的背景颜色,如图2-22所示。


图2-22 OutlineButton按钮的基本使用效果图
TextButton默认无背景填充,也无阴影高度,如图2-23所示。


图2-23 TextButton按钮的基本使用效果图
TextButton按钮与FlatButton不同的是,FlatButton按钮按下有高亮颜色显示,而TextButton只有水波纹显示,两者默认使用的颜色也不相同。
ElevatedButton默认情况下会带有圆角边框背景,当点击时,会有水波纹颜色与阴影,如图2-24所示。

图2-24 ElevatedButton按钮的基本使用效果图

图2-25为OutlinedButton的效果图,与OutlineButton定义非常相似,但两者有明显的视觉差异,OutlinedButton看起来更加舒适。

图2-25 OutlinedButton按钮的基本使用效果图

常用的IconButton系列按钮有BackButton、CloseButton、PopupMenuButton,如图2-26所示,BackButton按钮默认配置了后退的图标,基本使用代码如下。

如图2-27所示,CloseButton按钮默认配置了一个关闭图标,基本使用代码如下。


图2-26 BackButton的使用效果图

图2-27 CloseButton的使用效果图
PopupMenuButton是弹出的菜单按钮,基本使用代码如下。



2-26 Popup-MenuButton基本使用效果图