1.介绍
FAB,在Material Design中,一般用来处理界面中最常用,最基础的用户动作。它一般出现在屏幕内容的前面,通常是一个圆形,中间有一个图标。 FAB有三种类型:regular, mini, and extended。不要强行使用FAB,只有当使用场景符合FAB功能的时候使用才最为恰当。
2.使用原则
A FAB represents the primary action on a screen.
A FAB should perform a constructive action (such as create, share, or explore).
A FAB should be relevant to the screen on which it appears.
3.类结构
![](https://img.haomeiwen.com/i18683709/29b16389d8014a28.png)
4.属性值
![](https://img.haomeiwen.com/i18683709/69d0e0f57944170a.png)
属性 意义
child 子视图,一般为Icon,不推荐使用文字
tooltip FAB的文字解释,FAB被长按时显示,也是无障碍功能
foregroundColor 前景色
backgroundColor 背景色
heroTag hero效果使用的tag,系统默认会给所有FAB使用同一个tag,方便做动画效果
elevation 未点击时阴影值,默认6.0
hignlightElevation 点击时阴影值,默认12.0
onPressed 点击事件回调
mini 是否为“mini”类型,默认为false
shape 定义FAB的shape,设置shape时,默认的elevation将会失效,默认为CircleBorder
isExtended 是否为”extended”类型
5.基本使用
示例
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: new Builder(builder: (BuildContext context) {
return new FloatingActionButton(
child: const Icon(Icons.add),
tooltip: "Hello",
foregroundColor: Colors.white,
backgroundColor: Colors.black,
heroTag: null,
elevation: 7.0,
highlightElevation: 14.0,
onPressed: () {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("FAB is Clicked"),
));
},
mini: false,
shape: new CircleBorder(),
isExtended: false,
);
}),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
![](https://img.haomeiwen.com/i18683709/522dc80e926026b9.png)
6.属性分析
mini
FAB 分为三种类型:regular, mini, and extended。大小限制如下
const BoxConstraints _kSizeConstraints = const BoxConstraints.tightFor(
width: 56.0,
height: 56.0,
);
const BoxConstraints _kMiniSizeConstraints = const BoxConstraints.tightFor(
width: 40.0,
height: 40.0,
);
const BoxConstraints _kExtendedSizeConstraints = const BoxConstraints(
minHeight: 48.0,
maxHeight: 48.0,
);
regular和mini两种类型通过默认的构造方法实现。
extended类型则是通过
new FloatingActionButton.extended()
来实现。从参数上看差异并不大,只是把默认构造方法中的child换成了icon和label,不过通过下面的代码可以看到,传入的label和icon也是用来构建child的,不过使用的是Row来做一层包装而已。
![](https://img.haomeiwen.com/i18683709/a02297174c72a1c6.png)
(1)regular
![](https://img.haomeiwen.com/i18683709/c4e6e747c2d4a808.png)
(2)mini
![](https://img.haomeiwen.com/i18683709/da241fe366389df6.png)
(3)extended
![](https://img.haomeiwen.com/i18683709/473d64d4c2e49bb0.png)
7.heroTag
先看看heroTag对应的注释
/// The tag to apply to the button's [Hero] widget.
///
/// Defaults to a tag that matches other floating action buttons.
///
/// Set this to null explicitly if you don't want the floating action button to
/// have a hero tag.
///
/// If this is not explicitly set, then there can only be one
/// [FloatingActionButton] per route (that is, per screen), since otherwise
/// there would be a tag conflict (multiple heroes on one route can't have the
/// same tag). The material design specification recommends only using one
/// floating action button per screen.
首先我们了解下Hero,简单理解为两个界面内拥有同样tag的元素在界面切换过程中,会有动画效果,是界面切换不再那么生硬。
这里的heroTag就是用来标识FAB的。看下面的两个对比图。
(1)不设置heroTag属性,使用系统自带的tag值。
![](https://img.haomeiwen.com/i18683709/ef3beb5923ed6cf1.png)
https://img-blog.csdn.net/20180720072205514
(2)设置heroTag: null, 不使用系统自带的tag值。
![](https://img.haomeiwen.com/i18683709/eb46017f18267e8e.png)
从上面的效果图,明显可以看出界面之间切换的友好性还是存在很大差距的。若两个界面中均有FAB,建议不设置heroTag为null。避免界面切换的生硬。
8.floatingActionButtonLocation
这是Scaffold Widget的一个属性,和FAB配合使用。代表FAB在界面中四种不同的拜访方式
static const FloatingActionButtonLocation endFloat = const _EndFloatFabLocation();
static const FloatingActionButtonLocation centerFloat = const _CenterFloatFabLocation();
static const FloatingActionButtonLocation endDocked = const _EndDockedFloatingActionButtonLocation();
static const FloatingActionButtonLocation centerDocked = const _CenterDockedFloatingActionButtonLocation();
(1)endFloat
![](https://img.haomeiwen.com/i18683709/155b0ccfba9d06c8.png)
(2)centerFloat
![](https://img.haomeiwen.com/i18683709/92e76dd99a0187ab.png)
(3)endDocked
![](https://img.haomeiwen.com/i18683709/0f9fa54240ed6854.png)
(4)centerDocked
![](https://img.haomeiwen.com/i18683709/6c4b1814de43cdbd.png)
9.centerDocker&BottomNavigationBar
return new Scaffold(
floatingActionButton: new Builder(builder: (BuildContext context) {
return new FloatingActionButton(
child: const Icon(Icons.add),
tooltip: "Hello",
heroTag: null,
foregroundColor: Colors.white,
backgroundColor: Colors.black,
elevation: 7.0,
highlightElevation: 14.0,
);
}),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
// sets the background color of the `BottomNavigationBar`
canvasColor: Colors.black12,
// sets the active color of the `BottomNavigationBar` if `Brightness` is light
primaryColor: Colors.red,
textTheme: Theme.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.white))),
child: new BottomNavigationBar(
fixedColor: Colors.lightBlue,
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.home), title: new Text("每日干货")),
new BottomNavigationBarItem(
icon: new Icon(Icons.category),
title: new Text("分类阅读"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.whatshot),
title: new Text("匠心写作"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.search),
title: new Text("搜索"),
),
],
type: BottomNavigationBarType.fixed,
onTap: (int selected) {
setState(() {
});
},
),
),
);
![](https://img.haomeiwen.com/i18683709/fb3c3971a054b8ac.png)
网友评论