|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:getwidget/getwidget.dart'; |
| 3 | + |
| 4 | +typedef StickyHeaderWidgetBuilder = Widget Function( |
| 5 | + BuildContext context, double stuckValue); |
| 6 | + |
| 7 | +/// Place this widget inside a [ListView], [GridView], [CustomScrollView], [SingleChildScrollView] or similar. |
| 8 | +
|
| 9 | +class GFStickyHeaderBuilder extends StatefulWidget { |
| 10 | + /// Constructs a new [GFStickyHeaderBuilder] widget. |
| 11 | + const GFStickyHeaderBuilder({ |
| 12 | + Key key, |
| 13 | + @required this.stickyContentBuilder, |
| 14 | + @required this.content, |
| 15 | + this.direction = Axis.vertical, |
| 16 | + this.enableHeaderOverlap = false, |
| 17 | + this.callback, |
| 18 | + this.stickyContentPosition = GFPosition.start, |
| 19 | + }) : assert(direction != null), |
| 20 | + super(key: key); |
| 21 | + |
| 22 | + /// widget can be used to define [stickyContentBuilder]. |
| 23 | + final StickyHeaderWidgetBuilder stickyContentBuilder; |
| 24 | + |
| 25 | + /// widget can be used to define [content]. |
| 26 | + final Widget content; |
| 27 | + |
| 28 | + /// On state true, the [stickyContentBuilder] will overlap the [content]. |
| 29 | + /// Only works when direction is [Axis.vertical]. Default set to false. |
| 30 | + final bool enableHeaderOverlap; |
| 31 | + |
| 32 | + /// [GFPosition] allows to [stickyContentPosition] to stick at top in [Axis.vertical] and stick at start in [Axis.horizontal] |
| 33 | + /// Defaults to [GFPosition.start] |
| 34 | + final GFPosition stickyContentPosition; |
| 35 | + |
| 36 | + /// Allows to add custom stickyHeader stuck offset value |
| 37 | + final RenderGFStickyHeaderCallback callback; |
| 38 | + |
| 39 | + /// [direction] allows children to align in vertical / horizontal way |
| 40 | + /// Defaults to [Axis.vertical] |
| 41 | + final Axis direction; |
| 42 | + |
| 43 | + @override |
| 44 | + _GFStickyHeaderBuilderState createState() => _GFStickyHeaderBuilderState(); |
| 45 | +} |
| 46 | + |
| 47 | +class _GFStickyHeaderBuilderState extends State<GFStickyHeaderBuilder> { |
| 48 | + double _stuckValue; |
| 49 | + |
| 50 | + @override |
| 51 | + Widget build(BuildContext context) => GFStickyHeader( |
| 52 | + enableHeaderOverlap: widget.enableHeaderOverlap, |
| 53 | + direction: widget.direction, |
| 54 | + stickyContentPosition: widget.stickyContentPosition, |
| 55 | + stickyContent: LayoutBuilder( |
| 56 | + builder: (context, _) => |
| 57 | + widget.stickyContentBuilder(context, _stuckValue ?? 0.0), |
| 58 | + ), |
| 59 | + content: widget.content, |
| 60 | + callback: (double stuckValue) { |
| 61 | + if (_stuckValue != stuckValue) { |
| 62 | + _stuckValue = stuckValue; |
| 63 | + WidgetsBinding.instance.endOfFrame.then((_) { |
| 64 | + if (mounted) { |
| 65 | + setState(() {}); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + }, |
| 70 | + ); |
| 71 | +} |
0 commit comments