一个通用的滑动结构,可以指定滑动方向、是否反向、滑动控制器等属性。其中包含的子组件们必须是Sliver家族。

相关组件

SliverAppBar SliverList SliverGird

CustomScrollView基本使用

  1. <br />【slivers】 : 子组件列表 【List<Widget><br />【reverse】 : 是否反向 【bool】<br />【scrollDirection】 : 滑动方向 【Axis】<br />【controller】 : 控制器 【ScrollController】<br />![199.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589509735200-e777b313-82fe-49c4-9454-fb43fff4d7a3.gif#align=left&display=inline&height=774&margin=%5Bobject%20Object%5D&name=199.gif&originHeight=774&originWidth=414&size=3517338&status=done&style=none&width=414)
  1. import 'package:flutter/material.dart';
  2. class CustomScrollViewDemo extends StatelessWidget {
  3. final data = <Color>[
  4. Colors.purple[50],
  5. Colors.purple[100],
  6. Colors.purple[200],
  7. Colors.purple[300],
  8. Colors.purple[400],
  9. Colors.purple[500],
  10. Colors.purple[600],
  11. Colors.purple[700],
  12. Colors.purple[800],
  13. Colors.purple[900],
  14. ];
  15. @override
  16. Widget build(BuildContext context) {
  17. return Container(
  18. height: 300,
  19. child: CustomScrollView(
  20. anchor: 0,
  21. scrollDirection: Axis.vertical,
  22. reverse: false,
  23. slivers: <Widget>[_buildSliverAppBar(), _buildSliverFixedExtentList()],
  24. ),
  25. );
  26. }
  27. Widget _buildSliverFixedExtentList() => SliverFixedExtentList(
  28. itemExtent: 60,
  29. delegate: SliverChildBuilderDelegate(
  30. (_, int index) => Container(
  31. alignment: Alignment.center,
  32. width: 100,
  33. height: 50,
  34. color: data[index],
  35. child: Text(
  36. colorString(data[index]),
  37. style: TextStyle(color: Colors.white, shadows: [
  38. Shadow(
  39. color: Colors.black,
  40. offset: Offset(.5, .5),
  41. blurRadius: 2)
  42. ]),
  43. ),
  44. ),
  45. childCount: data.length),
  46. );
  47. String colorString(Color color) =>
  48. "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
  49. _buildSliverAppBar() {
  50. return SliverAppBar(
  51. expandedHeight: 190.0,
  52. leading: Container(
  53. margin: EdgeInsets.all(10),
  54. child: Image.asset('assets/images/icon_head.png')),
  55. flexibleSpace: FlexibleSpaceBar(
  56. //伸展处布局
  57. titlePadding: EdgeInsets.only(left: 55, bottom: 15), //标题边距
  58. collapseMode: CollapseMode.parallax, //视差效果
  59. title: Text(
  60. '张风捷特烈',
  61. style: TextStyle(color: Colors.black, //标题
  62. shadows: [
  63. Shadow(color: Colors.blue, offset: Offset(1, 1), blurRadius: 2)
  64. ]),
  65. ),
  66. background: Image.asset(
  67. "assets/images/caver.jpeg", fit: BoxFit.cover,
  68. ),
  69. ),
  70. );
  71. }
  72. }