Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
goderbauer review
  • Loading branch information
dnfield committed May 24, 2022
commit eeb4d1381f43ad2746bba0bc242891326dd8cb7f
116 changes: 53 additions & 63 deletions packages/visibility_detector/lib/src/render_visibility_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ mixin RenderVisibilityDetectorBase on RenderObject {
// the markNeedsPaint above will never cause the composition callback to
// fire and we could miss a hide event. This schedule will get
// over-written by subsequent updates in paint, if paint is called.
_scheduleUpdate(null, semanticBounds);
_scheduleUpdate(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe make the parameters a required optional one so this call side reads a little better...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to make it an optional optional :)

}
}

Expand All @@ -131,10 +131,10 @@ mixin RenderVisibilityDetectorBase on RenderObject {
if (kDebugMode) {
return _debugScheduleUpdateCount;
}
return 0;
return null;
}

void _scheduleUpdate(ContainerLayer? layer, Rect bounds) {
void _scheduleUpdate(ContainerLayer? layer) {
if (kDebugMode) {
_debugScheduleUpdateCount += 1;
}
Expand Down Expand Up @@ -215,6 +215,25 @@ mixin RenderVisibilityDetectorBase on RenderObject {
);
}

/// Used to get the bounds of the render object when it is time to update
/// clients about visibility.
Rect get bounds;

@override
void paint(PaintingContext context, Offset offset) {
if (onVisibilityChanged != null) {
_compositionCallbackCanceller?.call();
_compositionCallbackCanceller =
context.addCompositionCallback((Layer layer) {
assert(!debugDisposed!);
final ContainerLayer? container =
layer is ContainerLayer ? layer : layer.parent;
_scheduleUpdate(container);
});
}
super.paint(context, offset);
}

bool _disposed = false;
@override
void dispose() {
Expand All @@ -241,21 +260,8 @@ class RenderVisibilityDetector extends RenderProxyBox
@override
final Key key;

/// See [RenderObject.paint].
@override
void paint(PaintingContext context, Offset offset) {
if (onVisibilityChanged != null) {
_compositionCallbackCanceller?.call();
_compositionCallbackCanceller =
context.addCompositionCallback((Layer layer) {
assert(!debugDisposed!);
final ContainerLayer? container =
layer is ContainerLayer ? layer : layer.parent;
_scheduleUpdate(container, semanticBounds);
});
}
super.paint(context, offset);
}
Rect get bounds => semanticBounds;
}

/// The [RenderObject] corresponding to the [SliverVisibilityDetector] widget.
Expand All @@ -276,53 +282,37 @@ class RenderSliverVisibilityDetector extends RenderProxySliver
@override
final Key key;

/// See [RenderObject.paint].
@override
void paint(PaintingContext context, Offset offset) {
if (onVisibilityChanged != null) {
_compositionCallbackCanceller?.call();
_compositionCallbackCanceller =
context.addCompositionCallback((Layer layer) {
assert(!debugDisposed!);

Size widgetSize;
Offset widgetOffset;
switch (applyGrowthDirectionToAxisDirection(
constraints.axisDirection,
constraints.growthDirection,
)) {
case AxisDirection.down:
widgetOffset = Offset(0, -constraints.scrollOffset);
widgetSize =
Size(constraints.crossAxisExtent, geometry!.scrollExtent);
break;
case AxisDirection.up:
final startOffset = geometry!.paintExtent +
constraints.scrollOffset -
geometry!.scrollExtent;
widgetOffset = Offset(0, math.min(startOffset, 0));
widgetSize =
Size(constraints.crossAxisExtent, geometry!.scrollExtent);
break;
case AxisDirection.right:
widgetOffset = Offset(-constraints.scrollOffset, 0);
widgetSize =
Size(geometry!.scrollExtent, constraints.crossAxisExtent);
break;
case AxisDirection.left:
final startOffset = geometry!.paintExtent +
constraints.scrollOffset -
geometry!.scrollExtent;
widgetOffset = Offset(math.min(startOffset, 0), 0);
widgetSize =
Size(geometry!.scrollExtent, constraints.crossAxisExtent);
break;
}
final ContainerLayer? container =
layer is ContainerLayer ? layer : layer.parent;
_scheduleUpdate(container, widgetOffset & widgetSize);
});
Rect get bounds {
Size widgetSize;
Offset widgetOffset;
switch (applyGrowthDirectionToAxisDirection(
constraints.axisDirection,
constraints.growthDirection,
)) {
case AxisDirection.down:
widgetOffset = Offset(0, -constraints.scrollOffset);
widgetSize = Size(constraints.crossAxisExtent, geometry!.scrollExtent);
break;
case AxisDirection.up:
final startOffset = geometry!.paintExtent +
constraints.scrollOffset -
geometry!.scrollExtent;
widgetOffset = Offset(0, math.min(startOffset, 0));
widgetSize = Size(constraints.crossAxisExtent, geometry!.scrollExtent);
break;
case AxisDirection.right:
widgetOffset = Offset(-constraints.scrollOffset, 0);
widgetSize = Size(geometry!.scrollExtent, constraints.crossAxisExtent);
break;
case AxisDirection.left:
final startOffset = geometry!.paintExtent +
constraints.scrollOffset -
geometry!.scrollExtent;
widgetOffset = Offset(math.min(startOffset, 0), 0);
widgetSize = Size(geometry!.scrollExtent, constraints.crossAxisExtent);
break;
}
super.paint(context, offset);
return widgetOffset & widgetSize;
}
}
1 change: 0 additions & 1 deletion packages/visibility_detector/test/impression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ void main() {
child: VisibilityDetector(
key: UniqueKey(),
onVisibilityChanged: (info) {
print(info);
if (info.visibleFraction > .6) {
inView = true;
onFirstVis = 1;
Expand Down