flutter系列之:如丝般顺滑的SliverAppBar
简介
对于一个APP来说,肯定会有一个AppBar,这个AppBar一般包含了APP的导航信息等。虽然我们可以用一个固定的组件来做为AppBar,但是这样就会丢失很多特效,比如将AppBar固定在顶部,AppBar可以在滑动的过程中进行大小变换等。
当然这一切都不需要自己来实现,flutter已经为我们提供了一个非常强大的AppBar组件,这个组件叫做SliverAppBar。
SliverAppBar详解
我们先来看下SliverAppBar的定义:
class SliverAppBar extends StatefulWidget
可以看到SliverAppBar是一个StatefulWidget,它里面的状态包含的是一些配置信息,包括FloatingHeaderSnapConfiguration,OverScrollHeaderStretchConfiguration和PersistentHeaderShowOnScreenConfiguration等。
SliverAppBar可以接收很多属性,我们接下来会讲解其中最重要和最常用的几个属性。
- forceElevated
forceElevated是一个bool值,表示是否显示AppBar的阴影效果,默认值是false。
- primary
primary使用来配置AppBar的属性,表示AppBar是否显示在界面的Top位置。如果设置为true,那么AppBar将会被放置在Top的位置,并且使用的高度是系统status bar的高度。
- floating
floating是一个非常重要的属性,因为对于SliverAppBar来说,当界面向远离SliverAppBar的方向滚动的时候,SliverAppBar会隐藏或者缩写为status bar的高度,floating的意思就是当我们向SliverAppBar滑动的时候,SliverAppBar是否浮动展示。
- pinned
表示SliverAppBar在滚动的过程中是否会固定在界面的边缘。
- snap
snap是和floating一起使用的属性,snap表示当向SliverAppBar滚动的时候,SliverAppBar是否立即展示完全。
- automaticallyImplyLeading
automaticallyImplyLeading是用在AppBar中的属性,表示是否需要实现leading widget。
其中最常用的就是floating,pinned和snap这几个属性。
另外还有一个flexibleSpace组件,他是SliverAppBar在float的时候展示的widget,通常和expandedHeight配合使用。
SliverAppBar的使用
上面讲解了SliverAppBar的构造函数和基础属性,接下来我们通过具体的例子来讲解SliverAppBar如何使用。
通常来说SliverAppBar是和CustomScrollView一起使用的,也就是说SliverAppBar会被封装在CustomScrollView中。
CustomScrollView中除了SliverAppBar之外,还可以添加其他的sliver组件。
首先我们定义一个SliverAppBar:
SliverAppBar(
pinned: true,
snap: true,
floating: true,
expandedHeight: 200.0,
flexibleSpace: FlexibleSpaceBar(
title: const Text('SliverAppBar'),
background: Image.asset("images/head.jpg"),
),
),
这里我们设置pinned,snap和floating的值都为true,然后设置了expandedHeight和flexibleSpace。
这里的flexibleSpaces是一个FlexibleSpaceBar对象,这里我们设置了title和background属性。
接着我们需要把SliverAppBar放到CustomScrollView中进行展示。
Widget build(BuildContext context) {
return CustomScrollView(
slivers: <Widget>[
SliverAppBar(
...
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
color: index.isOdd ? Colors.white : Colors.green,
height: 100.0,
child: Center(
child: ListTile(
title: Text(
'1888888888' + index.toString(),
style: TextStyle(fontWeight: FontWeight.w500),
),
leading: Icon(
Icons.contact_phone,
color: Colors.blue[500],
),
),
),
);
},
childCount: 10,
),
),
],
);
在SliverAppBar之外,我们还提供了一个SliverList,这里使用了SliverChildBuilderDelegate来构造10个ListTile对象。
最后运行可以得到下面的界面:
默认情况下SliverAppBar是展开状态,如果我们将下面的SliverList向上滑动,flexibleSpace就会被隐藏,我们可以得到下面的界面:
当我们向上慢慢滑动的时候,因为设置的是floating=true, 并且snap=true,所以只要向上滑动,就会展示所有的flexibleSpace:
当我们将floating设置为false的时候,只有向上滑动到顶端的时候,flexibleSpace才会全部展示出来。
总结
简单点说,SliverAppBar就是一个在滑动中可变大小的AppBar,我们可以通过设置不同的参数来实现不同的效果。
相关文章
- flutter系列之:移动端的手势基础GestureDetector
- 【Flutter&Flame 游戏 - 壹】开启新世界的大门
- flutter weekly第9期
- Flutter-Getting Started
- flutter系列之:按比例缩放的AspectRatio和FractionallySizedBox
- Flutter中如何设置全局字体
- flutter系列之:Navigator的高级用法
- 2020年Flutter和 React Native如何选择
- flutter 队列
- flutter系列之:在flutter中使用导航Navigator
- flutter系列之:如丝般顺滑的SliverAppBar
- flutter系列之:移动端手势的具体使用
- flutter系列之:创建一个内嵌的navigation
- Flutter 开发 (1)iOS 下超详细集成 Flutter
- 【Flutter】Dart 数据类型 数字类型 ( Dart 文件创建 | num 类型 | int 类型 | double 类型 | num 相关 API )
- 【Flutter】Dart 技巧 ( 独立主函数入口 | 可空类型判定 | 默认值设定 )
- 【Flutter】StatefulWidget 组件 ( Image 组件 | TextField 组件 )
- 【错误记录】Flutter 报错 ( Could not read script ‘xxxflutter_toolsgradleapp_plugin_loader.gradle‘ )
- 【错误记录】Flutter 构建报错 ( Because xxx requires SDK version >=2.12.0-0 <3.0.0, versio | Dart SDK 版本低 )
- 【错误记录】Flutter 报错 ( Because xx depends on flutter_driver any from sdk which depends on args 1.6.0 )
- 【错误记录】Flutter 编译报错 ( The parameter ‘‘ can‘t have a value of ‘null‘ because of its type, but the im )
- 【Flutter】ListView 列表高级功能 ( ScrollController 上拉加载更多 )
- 【错误记录】Flutter 混合开发获取 BinaryMessenger 报错 ( FlutterActivityAndFragmentDelegate.getFlutterEngine() )
- 【Flutter】开发 Flutter 包和插件 ( Flutter 包和插件简介 | 创建 Flutter 插件 | 创建 Dart 包 )
- 【Flutter】Flutter 启动白屏问题 ( 问题描述 | 在 launch_background.xml 中设置启动过渡 UI )
- flutter系列之:如何自定义动画路由
- 在 Flutter 移动应用程序中创建一个列表