Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.70 KB | None | 0 0
  1. class StackedList extends StatelessWidget {
  2.   final List<Color> _colors = Colors.primaries;
  3.   static const _minHeight = 16.0;
  4.   static const _maxHeight = 120.0;
  5.  
  6.   @override
  7.   Widget build(BuildContext context) => CustomScrollView(
  8.         slivers: _colors
  9.             .map(
  10.               (color) => StackedListChild(
  11.                 minHeight: _minHeight,
  12.                 maxHeight: _colors.indexOf(color) == _colors.length - 1
  13.                     ? MediaQuery.of(context).size.height
  14.                     : _maxHeight,
  15.                 pinned: true,
  16.                 child: Container(
  17.                   color: _colors.indexOf(color) == 0
  18.                       ? Colors.black
  19.                       : _colors[_colors.indexOf(color) - 1],
  20.                   child: Container(
  21.                     decoration: BoxDecoration(
  22.                       borderRadius: BorderRadius.vertical(
  23.                           top: Radius.circular(_minHeight)),
  24.                       color: color,
  25.                     ),
  26.                   ),
  27.                 ),
  28.               ),
  29.             )
  30.             .toList(),
  31.       );
  32. }
  33.  
  34. class StackedListChild extends StatelessWidget {
  35.   final double minHeight;
  36.   final double maxHeight;
  37.   final bool pinned;
  38.   final bool floating;
  39.   final Widget child;
  40.  
  41.   SliverPersistentHeaderDelegate get _delegate => _StackedListDelegate(
  42.       minHeight: minHeight, maxHeight: maxHeight, child: child);
  43.  
  44.   const StackedListChild({
  45.     Key key,
  46.     @required this.minHeight,
  47.     @required this.maxHeight,
  48.     @required this.child,
  49.     this.pinned = false,
  50.     this.floating = false,
  51.   })  : assert(child != null),
  52.         assert(minHeight != null),
  53.         assert(maxHeight != null),
  54.         assert(pinned != null),
  55.         assert(floating != null),
  56.         super(key: key);
  57.  
  58.   @override
  59.   Widget build(BuildContext context) => SliverPersistentHeader(
  60.       key: key, pinned: pinned, floating: floating, delegate: _delegate);
  61. }
  62.  
  63. class _StackedListDelegate extends SliverPersistentHeaderDelegate {
  64.   final double minHeight;
  65.   final double maxHeight;
  66.   final Widget child;
  67.  
  68.   _StackedListDelegate({
  69.     @required this.minHeight,
  70.     @required this.maxHeight,
  71.     @required this.child,
  72.   });
  73.  
  74.   @override
  75.   double get minExtent => minHeight;
  76.  
  77.   @override
  78.   double get maxExtent => math.max(maxHeight, minHeight);
  79.  
  80.   @override
  81.   Widget build(
  82.       BuildContext context, double shrinkOffset, bool overlapsContent) {
  83.     return new SizedBox.expand(child: child);
  84.   }
  85.  
  86.   @override
  87.   bool shouldRebuild(_StackedListDelegate oldDelegate) {
  88.     return maxHeight != oldDelegate.maxHeight ||
  89.         minHeight != oldDelegate.minHeight ||
  90.         child != oldDelegate.child;
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement