新建项目-8.png

CustomScrollView

构造函数

  1. const CustomScrollView({
  2. Key key,
  3. Axis scrollDirection = Axis.vertical, //滑动的方向
  4. bool reverse = false, //控制 CustomScrollView 里列表项的排列顺序
  5. ScrollController controller, //可以控制 CustomScrollView 滚动的位置
  6. ScrollController 提供以下的几个功能:
  7. 1.设置 CustomScrollView 滑动的初始位置
  8. 2.可以控制 CustomScrollView 是否存储和恢复滑动的位置
  9. 3.可以读取、设置当前滑动的位置
  10. 可以继承 ScrollController 实现自定义的功能
  11. primary true 时,controller 必须为 null
  12. bool primary,
  13. ScrollPhysics physics,
  14. bool shrinkWrap = false,
  15. Key center, //放在 CustomScrollView 中间的 子Widget 的 key
  16. double anchor = 0.0, //CustomScrollView 开始滑动的偏移量
  17. 如果 anchor 0.0,则 CustomScrollView Widget 从头开始排列
  18. 如果 anchor 0.5,则 CustomScrollView Widget 从中间开始排列
  19. 如果 anchor 1.0,则 CustomScrollView Widget 从底部开始排列
  20. double cacheExtent,
  21. this.slivers = const <Widget>[],
  22. int semanticChildCount,
  23. DragStartBehavior dragStartBehavior = DragStartBehavior.start,//
  24. 确定处理拖动开始行为的方式。
  25. 如果设置为[DragStartBehavior.start],则在检测到拖动手势时将开始滚动拖动行为
  26. 如果设置为[DragStartBehavior.down],它将在首次检测到向下事件时开始 可选
  27. })

SliverAppBar

基本和AppBar一样,只是他只能在CustomScrollView中使用,应该很常见,滑动的时候固定appbar,就需要用到他.

  1. const SliverAppBar({
  2. Key key,
  3. this.leading, //左上角添加组件
  4. this.automaticallyImplyLeading = true,
  5. this.title,
  6. this.actions, //右上角添加组件
  7. this.flexibleSpace, //扩展的组件
  8. this.bottom, //
  9. this.elevation,
  10. this.forceElevated = false,
  11. this.backgroundColor,
  12. this.brightness,
  13. this.iconTheme,
  14. this.actionsIconTheme,
  15. this.textTheme,
  16. this.primary = true,
  17. this.centerTitle,
  18. this.titleSpacing = NavigationToolbar.kMiddleSpacing,
  19. this.expandedHeight,
  20. this.floating = false,
  21. this.pinned = false,
  22. this.snap = false,
  23. this.stretch = false,
  24. this.stretchTriggerOffset = 100.0,
  25. this.onStretchTrigger,
  26. this.shape,
  27. })

大部分和appbar一样,主要说下重要的几个:

pinned

是否将导航栏部分固定在appbar的位置.这个需求在实际中很常用. 看看效果图.

当 pinned = false:
**
2020-04-13 17.41.58.gif

可以看到appbar并没有固定在最上面,而是根据内容划出了界面.

当 pinned = true :
**
2020-04-13 17.43.50.gif

这个应该是需求中经常用到的效果了.

floating

当floating = false :

2020-04-13 17.48.02.gif

当floating = true:

2020-04-13 17.51.30.gif

仔细看 ,区别是在列表整体向下滑动时,appbar开始显示的位置不同.
当为false时,向下滑动时,会先降列表内容滑动顶部,然后appbar会跟着列表滑动显示出来.如上图
当为true时,向下滑动时,appbar会先跟着列表滑动显示出来. 然后继续列表的滑动. 如上图

snap

不能单独使用要配合 floating 和 pinned
具体效果看官网地址

stretch

是否展开,默认false,直接看值为true的效果图,就明白了.

2020-04-13 18.05.55.gif

flexibleSpace

可以再里面添加扩展的内容:

  1. flexibleSpace: FlexibleSpaceBar(
  2. title: Text('开学季 Pinned 效果'),
  3. background: Image.network(
  4. "https://goss.cfp.cn/creative/vcg/800/new/VCG211165042753.jpg",
  5. fit: BoxFit.cover,
  6. ),
  7. ),
  1. flexibleSpace: Row(
  2. children: <Widget>[
  3. Expanded(
  4. child: Image.network('https://goss.cfp.cn/creative/vcg/800/new/VCG211165042753.jpg',fit: BoxFit.cover,),
  5. )
  6. ],
  7. ),

通过测试发现 后面不是 FlexibleSpaceBar 的话, stretch = true 无效.
**

SliverPadding

和Padding一样.子控件是 sliver 类型…

  1. const SliverPadding({
  2. Key key,
  3. @required this.padding,
  4. Widget sliver,
  5. })

列子:

  1. SliverPadding(
  2. padding: EdgeInsets.only(top: 10),
  3. sliver: SliverToBoxAdapter(
  4. child: Container(
  5. height: 66,
  6. color: Colors.cyanAccent,
  7. ),
  8. ),
  9. ),

2020-04-13 18.28.54.gif

上图中在padding中添加了一个背景色为青色的容器widget

SliverToBoxAdapter

  1. const SliverToBoxAdapter({
  2. Key key,
  3. Widget child,
  4. })

里面可以设置不是sliver类型的widget。如上图中的 padding中添加的 container

SliverGrid


  1. const SliverGrid({
  2. Key key,
  3. @required SliverChildDelegate delegate,
  4. @required this.gridDelegate,
  5. })

就两个协议,一个是布局协议一个展示协议.基本和GridView一样.也有count和extext… 不设置个数默认无数个
SliverChildListDelegate 这种方式前提是知道cell个数,比较少,好搭建
SliverChildBuilderDelegate 这种方式,可以根据数组去创建,不知道cell个数

SliverFixedExtentList

  1. const SliverFixedExtentList({
  2. Key key,
  3. @required SliverChildDelegate delegate,
  4. @required this.itemExtent,
  5. }) :

和listview差不多.也是协议 不设置个数默认无数个

例子:

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class MinePage extends StatefulWidget {
  4. @override
  5. _MinePageState createState() => _MinePageState();
  6. }
  7. class _MinePageState extends State<MinePage> {
  8. @override
  9. Widget build(BuildContext context) {
  10. return CustomScrollView(
  11. slivers: <Widget>[
  12. SliverAppBar(
  13. pinned: true,
  14. stretch: true,
  15. expandedHeight: 200.0,
  16. flexibleSpace: FlexibleSpaceBar(
  17. title: Text('开学季'),
  18. background: Image.network(
  19. "https://goss.cfp.cn/creative/vcg/800/new/VCG211165042753.jpg",
  20. fit: BoxFit.cover,
  21. ),
  22. ),
  23. ),
  24. SliverPadding(padding: EdgeInsets.only(top: 10),),
  25. SliverGrid(
  26. //调整间距
  27. gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
  28. crossAxisSpacing: 10,
  29. maxCrossAxisExtent: 200,
  30. mainAxisSpacing: 10,
  31. childAspectRatio: 4.0
  32. ),
  33. //加载内容
  34. delegate: SliverChildBuilderDelegate(
  35. (context,index) {
  36. return Container( //设置每个item的内容
  37. alignment: Alignment.center,
  38. color: Colors.orangeAccent,
  39. child: Text("$index"),
  40. );
  41. },
  42. childCount: 20, //设置个数
  43. ),
  44. ),
  45. SliverPadding(padding: EdgeInsets.only(top: 10),),
  46. SliverFixedExtentList(
  47. itemExtent: 50,
  48. //加载内容
  49. delegate: SliverChildBuilderDelegate(
  50. (context, index) {
  51. return Container(
  52. alignment: Alignment.center,
  53. color: Colors.deepPurpleAccent,
  54. child: Text('$index'),
  55. );
  56. },
  57. //设置显示个数
  58. childCount: 20,
  59. ),
  60. )
  61. ],
  62. );
  63. }
  64. }

2020-04-13 18.14.54.gif