|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +class SliverCustomHeaderDelegate extends SliverPersistentHeaderDelegate { |
| 4 | + final double collapsedHeight; |
| 5 | + final double expandedHeight; |
| 6 | + final double paddingTop; |
| 7 | + final Widget child; |
| 8 | + final String title; |
| 9 | + |
| 10 | + SliverCustomHeaderDelegate({ |
| 11 | + this.collapsedHeight, |
| 12 | + this.expandedHeight, |
| 13 | + this.paddingTop, |
| 14 | + this.child, |
| 15 | + this.title, |
| 16 | + }); |
| 17 | + |
| 18 | + @override |
| 19 | + double get minExtent => this.collapsedHeight + this.paddingTop; |
| 20 | + |
| 21 | + @override |
| 22 | + double get maxExtent => this.expandedHeight; |
| 23 | + |
| 24 | + @override |
| 25 | + bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) { |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + Color makeStickyHeaderBgColor(shrinkOffset) { |
| 30 | + final int alpha = (shrinkOffset / (this.maxExtent - this.minExtent) * 255) |
| 31 | + .clamp(0, 255) |
| 32 | + .toInt(); |
| 33 | + return Color.fromARGB(alpha, 255, 255, 255); |
| 34 | + } |
| 35 | + |
| 36 | + Color makeStickyHeaderTextColor(shrinkOffset, isIcon) { |
| 37 | + if (shrinkOffset <= 50) { |
| 38 | + return isIcon ? Colors.white : Colors.transparent; |
| 39 | + } else { |
| 40 | + final int alpha = (shrinkOffset / (this.maxExtent - this.minExtent) * 255) |
| 41 | + .clamp(0, 255) |
| 42 | + .toInt(); |
| 43 | + return Color.fromARGB(alpha, 0, 0, 0); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + Widget build( |
| 49 | + BuildContext context, double shrinkOffset, bool overlapsContent) { |
| 50 | + return Container( |
| 51 | + height: this.maxExtent, |
| 52 | + width: MediaQuery.of(context).size.width, |
| 53 | + child: Stack( |
| 54 | + fit: StackFit.expand, |
| 55 | + children: <Widget>[ |
| 56 | + // 背景图 |
| 57 | + this.child, |
| 58 | + // 收起头部 |
| 59 | + Positioned( |
| 60 | + left: 0, |
| 61 | + right: 0, |
| 62 | + top: 0, |
| 63 | + child: Container( |
| 64 | + color: this.makeStickyHeaderBgColor(shrinkOffset), // 背景颜色 |
| 65 | + child: SafeArea( |
| 66 | + bottom: false, |
| 67 | + child: Container( |
| 68 | + height: this.collapsedHeight, |
| 69 | + child: Row( |
| 70 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 71 | + children: <Widget>[ |
| 72 | + IconButton( |
| 73 | + icon: Icon( |
| 74 | + Icons.arrow_back_ios, |
| 75 | + color: this.makeStickyHeaderTextColor( |
| 76 | + shrinkOffset, true), // 返回图标颜色 |
| 77 | + ), |
| 78 | + onPressed: () => Navigator.pop(context), |
| 79 | + ), |
| 80 | + Text( |
| 81 | + this.title, |
| 82 | + style: TextStyle( |
| 83 | + fontSize: 18, |
| 84 | + color: this.makeStickyHeaderTextColor( |
| 85 | + shrinkOffset, false), // 标题颜色 |
| 86 | + ), |
| 87 | + ), |
| 88 | + IconButton( |
| 89 | + icon: Icon( |
| 90 | + Icons.share, |
| 91 | + color: Colors.transparent, // 分享图标颜色 |
| 92 | + ), |
| 93 | + onPressed: () {}, |
| 94 | + ), |
| 95 | + ], |
| 96 | + ), |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ], |
| 102 | + ), |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments