Flutter--SliverList组件

一、SliverList的简介

要同时滚动ListView和GridView的时候可以使用SliverList和SliverGrid

SliverList需要和CustomScrollView配合使用

二、SliverList的源码

const SliverList({
    Key key,
    @required SliverChildDelegate delegate,
  }) : super(key: key, delegate: delegate);

三、SliverList的属性

delegate:SliverChildDelegate 系统提供个两个已经实现好的代理:SliverChildListDelegate/SliverChildBuilderDelegate

四、SliverChildListDelegate的源码

SliverChildListDelegate(
    this.children, {
    this.addAutomaticKeepAlives = true,
    this.addRepaintBoundaries = true,
    this.addSemanticIndexes = true,
    this.semanticIndexCallback = _kDefaultSemanticIndexCallback,
    this.semanticIndexOffset = 0,
  }) : assert(children != null),
       assert(addAutomaticKeepAlives != null),
       assert(addRepaintBoundaries != null),
       assert(addSemanticIndexes != null),
       assert(semanticIndexCallback != null),
       _keyToIndex = <Key, int>{null: 0};

五、SliverChildBuilderDelegate的源码

const SliverChildBuilderDelegate(
    this.builder, {
    this.findChildIndexCallback,
    this.childCount,
    this.addAutomaticKeepAlives = true,
    this.addRepaintBoundaries = true,
    this.addSemanticIndexes = true,
    this.semanticIndexCallback = _kDefaultSemanticIndexCallback,
    this.semanticIndexOffset = 0,
  }) : assert(builder != null),
       assert(addAutomaticKeepAlives != null),
       assert(addRepaintBoundaries != null),
       assert(addSemanticIndexes != null),
       assert(semanticIndexCallback != null);

六、demo

6.1、SliverList属性delegate:SliverChildBuilderDelegate的demo

class _SliverListFulState extends State<SliverListFul> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("SliverList学习"),
          ),
          body:CustomScrollView(
            slivers: [
              SliverList(
                delegate: SliverChildBuilderDelegate((content, index) {
                  return Container(
                    height: 65,
                    color: Colors.primaries[index % Colors.primaries.length],
                  );
                }, childCount: 20),
              ),
            ],
          )

          ),
    );
  }
}

6.2、SliverList属性delegate:SliverChildListDelegate的demo

class _SliverListFulState extends State<SliverListFul> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("SliverList学习"),
          ),
          body: CustomScrollView(
            slivers: [
              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],
                  ),
                ]),
              ),
            ],
          )),
    );
  }
}



作者:小迷糊_dcee
链接:https://www.jianshu.com/p/50d8c1ea8531
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值