基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom_sliver_persistent_header_delegate.dart 1.2 KiB

1234567891011121314151617181920212223242526272829303132
  1. // 自定义 SliverPersistentHeaderDelegate
  2. import 'package:flutter/material.dart';
  3. class CustomSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  4. final double max; // 最大高度
  5. final double min; // 最小高度
  6. final Widget child; // 需要展示的内容
  7. CustomSliverPersistentHeaderDelegate({@required this.max, @required this.min, @required this.child})
  8. // 如果 assert 内部条件不成立,会报错
  9. : assert(max != null),
  10. assert(min != null),
  11. assert(child != null),
  12. assert(min <= max),
  13. super();
  14. // 返回展示的内容,如果内容固定可以直接在这定义,如果需要可扩展,这边通过传入值来定义
  15. @override
  16. Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) => child;
  17. @override
  18. double get maxExtent => max; // 返回最大高度
  19. @override
  20. double get minExtent => min; // 返回最小高度
  21. @override
  22. bool shouldRebuild(CustomSliverPersistentHeaderDelegate oldDelegate) {
  23. // 是否需要更新,这里我们定义当高度范围和展示内容被替换的时候进行刷新界面
  24. return max != oldDelegate.max || min != oldDelegate.min || child != oldDelegate.child;
  25. }
  26. }