Skip to content

[visibility_detector] add custom visible bounds #523

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

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ mixin RenderVisibilityDetectorBase on RenderObject {
/// The key for the corresponding [VisibilityDetector] widget.
Key get key;

/// The customVisibleBounds for the corresponding [VisibilityDetector] widget.
Rect? get customVisibleBounds;

VoidCallback? _compositionCallbackCanceller;

VisibilityChangedCallback? _onVisibilityChanged;
Expand Down Expand Up @@ -208,7 +211,7 @@ mixin RenderVisibilityDetectorBase on RenderObject {
ancestor = ancestor.parent;
}

Rect clip = Rect.largest;
var clip = customVisibleBounds ?? Rect.largest;
for (int index = ancestors.length - 1; index > 0; index -= 1) {
final parent = ancestors[index];
final child = ancestors[index - 1];
Expand Down Expand Up @@ -283,6 +286,7 @@ class RenderVisibilityDetector extends RenderProxyBox
RenderBox? child,
required this.key,
required VisibilityChangedCallback? onVisibilityChanged,
this.customVisibleBounds,
}) : assert(key != null),
super(child) {
_onVisibilityChanged = onVisibilityChanged;
Expand All @@ -291,6 +295,9 @@ class RenderVisibilityDetector extends RenderProxyBox
@override
final Key key;

@override
Rect? customVisibleBounds;

@override
Rect? get bounds => hasSize ? semanticBounds : null;
}
Expand All @@ -306,13 +313,17 @@ class RenderSliverVisibilityDetector extends RenderProxySliver
RenderSliver? sliver,
required this.key,
required VisibilityChangedCallback? onVisibilityChanged,
this.customVisibleBounds,
}) : super(sliver) {
_onVisibilityChanged = onVisibilityChanged;
}

@override
final Key key;

@override
Rect? customVisibleBounds;

@override
Rect? get bounds {
if (geometry == null) {
Expand Down
18 changes: 16 additions & 2 deletions packages/visibility_detector/lib/src/visibility_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ class VisibilityDetector extends SingleChildRenderObjectWidget {
required Key key,
required Widget child,
required this.onVisibilityChanged,
this.customVisibleBounds,
}) : assert(key != null),
assert(child != null),
super(key: key, child: child);

/// The callback to invoke when this widget's visibility changes.
final VisibilityChangedCallback? onVisibilityChanged;

/// Custom visible bounds to use for this widget.
final Rect? customVisibleBounds;

/// See [RenderObjectWidget.createRenderObject].
@override
RenderVisibilityDetector createRenderObject(BuildContext context) {
return RenderVisibilityDetector(
key: key!,
onVisibilityChanged: onVisibilityChanged,
customVisibleBounds: customVisibleBounds,
);
}

Expand All @@ -52,7 +57,9 @@ class VisibilityDetector extends SingleChildRenderObjectWidget {
void updateRenderObject(
BuildContext context, RenderVisibilityDetector renderObject) {
assert(renderObject.key == key);
renderObject.onVisibilityChanged = onVisibilityChanged;
renderObject
..onVisibilityChanged = onVisibilityChanged
..customVisibleBounds = customVisibleBounds;
}
}

Expand All @@ -68,19 +75,24 @@ class SliverVisibilityDetector extends SingleChildRenderObjectWidget {
required Key key,
required Widget sliver,
required this.onVisibilityChanged,
this.customVisibleBounds,
}) : assert(key != null),
assert(sliver != null),
super(key: key, child: sliver);

/// The callback to invoke when this widget's visibility changes.
final VisibilityChangedCallback? onVisibilityChanged;

/// Custom visible bounds to use for this widget.
final Rect? customVisibleBounds;

/// See [RenderObjectWidget.createRenderObject].
@override
RenderSliverVisibilityDetector createRenderObject(BuildContext context) {
return RenderSliverVisibilityDetector(
key: key!,
onVisibilityChanged: onVisibilityChanged,
customVisibleBounds: customVisibleBounds,
);
}

Expand All @@ -89,7 +101,9 @@ class SliverVisibilityDetector extends SingleChildRenderObjectWidget {
void updateRenderObject(
BuildContext context, RenderSliverVisibilityDetector renderObject) {
assert(renderObject.key == key);
renderObject.onVisibilityChanged = onVisibilityChanged;
renderObject
..onVisibilityChanged = onVisibilityChanged
..customVisibleBounds = customVisibleBounds;
}
}

Expand Down