Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the possibility to nest multiple sticky headers #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/sticky_headers/render.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ class RenderStickyHeader extends RenderBox
RenderStickyHeaderCallback? _callback;
ScrollPosition _scrollPosition;
bool _overlapHeaders;
double _offset;

RenderStickyHeader({
required ScrollPosition scrollPosition,
RenderStickyHeaderCallback? callback,
bool overlapHeaders = false,
RenderBox? header,
RenderBox? content,
double? offset,
}) : _scrollPosition = scrollPosition,
_callback = callback,
_offset = offset ?? 0,
_overlapHeaders = overlapHeaders {
if (content != null) add(content);
if (header != null) add(header);
Expand All @@ -62,6 +65,14 @@ class RenderStickyHeader extends RenderBox
markNeedsLayout();
}

set offset(double newValue) {
if (_offset == newValue) {
return;
}
_offset = newValue;
markNeedsLayout();
}

set overlapHeaders(bool newValue) {
if (_overlapHeaders == newValue) {
return;
Expand Down Expand Up @@ -135,7 +146,7 @@ class RenderStickyHeader extends RenderBox
final scrollBox = _scrollPosition.context.notificationContext!.findRenderObject();
if (scrollBox?.attached ?? false) {
try {
return localToGlobal(Offset.zero, ancestor: scrollBox).dy;
return localToGlobal(Offset.zero, ancestor: scrollBox).dy - _offset;
} catch (e) {
// ignore and fall-through and return 0.0
}
Expand Down
4 changes: 4 additions & 0 deletions lib/sticky_headers/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ class StickyHeader extends MultiChildRenderObjectWidget {

@override
void updateRenderObject(BuildContext context, RenderStickyHeader renderObject) {
// If we find a parent StickyHeader we look for its size to provide an offset
// to current StickyHeader
final parent = context.findAncestorRenderObjectOfType<RenderStickyHeader>();
final scrollPosition = controller?.position ?? Scrollable.of(context)!.position;
renderObject
..scrollPosition = scrollPosition
..callback = callback
..offset = parent?.lastChild?.size.height ?? 0
..overlapHeaders = overlapHeaders;
}
}
Expand Down