前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter之SliverAppBar

Flutter之SliverAppBar

作者头像
老孟Flutter
发布2020-09-11 15:49:15
1.4K0
发布2020-09-11 15:49:15
举报
文章被收录于专栏:Flutter

SliverAppBar控件可以实现页面头部区域展开、折叠的效果,类似于Android中的CollapsingToolbarLayout。

先看下SliverAppBar实现的效果,效果图如下:

SliverAppBar控件需要和CustomScrollView搭配使用,SliverAppBar要通常放在slivers的第一位,后面接其他sliver控件。

代码语言:javascript
复制
CustomScrollView(
      slivers: <Widget>[
        SliverAppBar(
          
        ),
        //其他sliver控件
      ],
    )

SliverAppBar和其他slivers控件的结构如下:

SliverAppBar中有一个非常重要的参数flexibleSpace,flexibleSpace是SliverAppBar中展开和折叠区域,flexibleSpace与expandedHeight一起使用,

expandedHeight表示flexibleSpace的高度,

代码语言:javascript
复制
SliverAppBar(
          expandedHeight: 200.0,
          flexibleSpace: FlexibleSpaceBar(
          
          ),
        ),

SliverAppBar其他常用属性说明如下:

属性

说明

leading

左侧控件,通常情况下为"返回"图标

title

标题,通常为Text控件

actions

右侧控件

flexibleSpace

展开和折叠区域

bottom

底部控件

elevation‍

阴影

expandedHeight

展开区域的高度‍‍

floating

设置为true时,向下滑动时,即使当前CustomScrollView不在顶部,SliverAppBar也会跟着一起向下出现

pinned

设置为true时,当SliverAppBar内容滑出屏幕时,将始终渲染一个固定在顶部的收起状态

snap

设置为true时,当手指放开时,SliverAppBar会根据当前的位置进行调整,始终保持展开或收起的状态,此效果在floating=true时生效

实现文章开头效果的整体代码如下:

代码语言:javascript
复制
class SliverAppBarDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: <Widget>[
        SliverAppBar(
          pinned: true,
          expandedHeight: 200.0,
          flexibleSpace: FlexibleSpaceBar(
            title: Text('复仇者联盟'),
            background: Image.network(
              'http://img.haote.com/upload/20180918/2018091815372344164.jpg',
              fit: BoxFit.fitHeight,
            ),
          ),
        ),
        SliverFixedExtentList(
          itemExtent: 80.0,
          delegate: SliverChildBuilderDelegate(
            (BuildContext context, int index) {
              return Card(
                child: Container(
                  alignment: Alignment.center,
                  color: Colors.primaries[(index % 18)],
                  child: Text(''),
                ),
              );
            },
          ),
        ),
      ],
    );
  }
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-01-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 老孟Flutter 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档