Flutter教学

Flutter(70):Sliver组件之CustomScrol

2020-10-26  本文已影响0人  starryxp

Flutter教学目录持续更新中

Github源代码持续更新中

1.CustomScrollView

一个使用slivers创建自定义的滚动效果的ScrollView
这个是Sliver组件中基础的组件,支持讲各种Sliver组件聚合在一起实现各种折叠效果,当然NestedScrollView也可以,后面会一一介绍。

2.CustomScrollView属性

= scrollDirection = Axis.vertical:滚动方向

CustomScrollView中很多属性跟ListView是一样的,这里就不再展开详细讲解了:
Flutter(33):Material组件之ListTile、RefreshIndicator、ListView、Divider

3. CustomScrollView

  _mySliverLayoutBuilder() {
    var _color = Colors.green;
    return CustomScrollView(
      scrollDirection: Axis.vertical,
      reverse: false,
      primary: true,
      anchor: 0.2,
      physics: BouncingScrollPhysics(),
      dragStartBehavior: DragStartBehavior.down,
      slivers: [
        SliverLayoutBuilder(
          builder: (BuildContext context, SliverConstraints constraints) {
            print('SliverConstraints  = $constraints');
            if (constraints.userScrollDirection == ScrollDirection.forward) {
              _color = Colors.blue;
            } else if (constraints.userScrollDirection ==
                ScrollDirection.idle) {
              _color = Colors.green;
            } else {
              _color = Colors.cyan;
            }

            return SliverToBoxAdapter(
              child: Container(
                height: 200,
                color: _color,
              ),
            );
          },
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate(
            (BuildContext context, int index) {
              return Card(
                child: Container(
                  height: 50,
                  color: Colors.primaries[(index % 18)],
                ),
              );
            },
            childCount: 15,
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('CustomScrollView'),
      ),
      body: _mySliverLayoutBuilder(),
    );
  }
image.png

下一节:Sliver组件之SliverAppBar

Flutter(71):Sliver组件之SliverAppBar

Flutter教学目录持续更新中

Github源代码持续更新中

上一篇 下一篇

猜你喜欢

热点阅读