Flutter CustomScrollView 的使用 及 常用的Sliver系列组件

CustomScrollView简介

CustomScrollView是可以使用Sliver来自定义滚动模型(效果)的组件。它可以包含多种滚动模型。包括header,footer,CustomScrollView可以实现把多个彼此独立的可滑动widget组合起来。

CustomScrollView 一"码"当先

  Widget mCustomScrollView() {
    return CustomScrollView(
      slivers: [
        SliverAppBar(
          floating: true,
          title: Text('CustomScrollView Demo'),
          expandedHeight: 300,
          flexibleSpace: Image.network(
            "https://img2.baidu.com/it/u=1977503081,1869926726&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
            fit: BoxFit.cover,
          ),
        ),
        SliverList(
            delegate: SliverChildListDelegate([
          Container(
            height: 80,
            color: Colors.primaries[0],
          ),
          Container(
            height: 80,
            color: Colors.primaries[1],
          ),
          Container(
            height: 80,
            color: Colors.primaries[2],
          ),
          Container(
            height: 80,
            color: Colors.primaries[3],
          ),
          Container(
            height: 80,
            color: Colors.primaries[4],
          ),
        ])),
        SliverAppBar(
          pinned: false,
          expandedHeight: 250.0,
          backgroundColor: Colors.blue,
          flexibleSpace: FlexibleSpaceBar(
            background: Image.network(
              "https://img2.baidu.com/it/u=1977503081,1869926726&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
              fit: BoxFit.cover,
            ),
          ),
        ),
      ],
    );
  }

效果:

CustomScrollView其实就是一个Sliver的载体,里面添加的子widget都必须是Sliver!!!接下来讲重点介绍Sliver

Sliver的概念

Flutter中提出一个Sliver(中文为“薄片”的意思)概念,如果一个可滚动组件支持Sliver模型,那么该滚动可以将子组件分成好多个“薄片”(Sliver),只有当Sliver出现在视口中时才会去构建它,这种模型也称为“基于Sliver的延迟构建模型”。可滚动组件中有很多都支持基于Sliver的延迟构建模型,如ListView、GridView,但是也有不支持该模
的,如SingleChildScrollView。

SliverList和SliverGrid

SliverList只有一个属性:delegate,类型是SliverChildDelegate。SliverChildDelegate是一个抽象类,不能直接使用。Flutter中定义好了两个继承于SliverChildDelegate的类对象,可以直接用,分别是:SliverChildListDelegate和SliverChildBuilderDelegate。

SliverChildListDelegate

先来看SliverChildListDelegate,声明如下:

SliverChildListDelegate(
    this.children, {
    .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值