Skip to content

Provide an optional choice for the caller: if the page disposal scena… #554

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 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
52 changes: 52 additions & 0 deletions packages/visibility_detector/lib/src/visibility_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:math' show max;
import 'package:flutter/widgets.dart';

import 'render_visibility_detector.dart';
import 'visibility_detector_element.dart';

/// A [VisibilityDetector] widget fires a specified callback when the widget
/// changes visibility.
Expand All @@ -31,13 +32,26 @@ class VisibilityDetector extends SingleChildRenderObjectWidget {
required Key key,
required Widget child,
required this.onVisibilityChanged,
this.isChangedOnDispose = true,
}) : assert(key != null),
assert(child != null),
super(key: key, child: child);

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

/// Indicates whether the visibility change callback should be triggered
/// when the widget is disposed.
///
/// When set to true, the `onVisibilityChanged` callback will be invoked
/// even after the widget has been removed from the widget tree. This can
/// be useful for scenarios where you need to perform cleanup or notify
/// other parts of the application about the visibility state before
/// the widget is completely disposed of.
///
/// Default value is true.
final bool isChangedOnDispose;

/// See [RenderObjectWidget.createRenderObject].
@override
RenderVisibilityDetector createRenderObject(BuildContext context) {
Expand All @@ -54,6 +68,22 @@ class VisibilityDetector extends SingleChildRenderObjectWidget {
assert(renderObject.key == key);
renderObject.onVisibilityChanged = onVisibilityChanged;
}

/// See [RenderObjectWidget.createElement].
///
/// This override creates a [VisibilityDetectorElement] configured with
/// the current [VisibilityDetector] widget instance. The created element
/// maintains the association between this widget and its corresponding
/// [RenderVisibilityDetector] render object.
///
/// Framework automatically calls this method when inflating the widget tree.
@override
SingleChildRenderObjectElement createElement() {
if (isChangedOnDispose) {
return super.createElement();
}
return VisibilityDetectorElement(this);
}
}

class SliverVisibilityDetector extends SingleChildRenderObjectWidget {
Expand All @@ -68,13 +98,26 @@ class SliverVisibilityDetector extends SingleChildRenderObjectWidget {
required Key key,
required Widget sliver,
required this.onVisibilityChanged,
this.isChangedOnDispose = true,
}) : assert(key != null),
assert(sliver != null),
super(key: key, child: sliver);

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

/// Indicates whether the visibility change callback should be triggered
/// when the widget is disposed.
///
/// When set to true, the `onVisibilityChanged` callback will be invoked
/// even after the widget has been removed from the widget tree. This can
/// be useful for scenarios where you need to perform cleanup or notify
/// other parts of the application about the visibility state before
/// the widget is completely disposed of.
///
/// Default value is true.
final bool isChangedOnDispose;

/// See [RenderObjectWidget.createRenderObject].
@override
RenderSliverVisibilityDetector createRenderObject(BuildContext context) {
Expand All @@ -91,6 +134,15 @@ class SliverVisibilityDetector extends SingleChildRenderObjectWidget {
assert(renderObject.key == key);
renderObject.onVisibilityChanged = onVisibilityChanged;
}

/// See [RenderObjectWidget.createElement].
@override
SingleChildRenderObjectElement createElement() {
if (isChangedOnDispose) {
return super.createElement();
}
return SliverVisibilityDetectorElement(this);
}
}

typedef VisibilityChangedCallback = void Function(VisibilityInfo info);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:flutter/widgets.dart';

import 'render_visibility_detector.dart';
import 'visibility_detector.dart';

/// An [Element] that manages the lifecycle of the associated [RenderVisibilityDetector].
///
/// This element is responsible for:
/// - Attaching/detaching the [RenderVisibilityDetector] to the render tree
/// - Clearing visibility callback references during unmounting to prevent memory leaks
/// - Coordinating visibility updates with [VisibilityDetectorController]
///
/// When this element is unmounted from the widget tree, it will:
/// 1. Nullify the [RenderVisibilityDetector.onVisibilityChanged] callback
/// 2. Trigger disposal of the associated [RenderVisibilityDetector]
/// 3. Release subscriptions to visibility tracking mechanisms
///
/// {@template visibility_detector_element_ownership}
/// This element is automatically created and managed by the framework when using
/// a [VisibilityDetector] widget. Developers should never need to instantiate this
/// class directly.
/// {@endtemplate}
///
/// See also:
/// - [VisibilityDetector], the widget that creates this element
/// - [RenderVisibilityDetector], the render object managed by this element
class VisibilityDetectorElement extends SingleChildRenderObjectElement {
VisibilityDetectorElement(VisibilityDetector widget) : super(widget);

@override
void unmount() {
final renderObject = this.renderObject;
if (renderObject is RenderVisibilityDetector) {
renderObject.onVisibilityChanged = null;
}
super.unmount();
}
}

/// An [Element] that manages the lifecycle of the associated [RenderSliverVisibilityDetector].
///
/// This element is responsible for:
/// - Attaching/detaching the [RenderSliverVisibilityDetector] to the render tree
/// - Clearing visibility callback references during unmounting to prevent memory leaks
/// - Coordinating visibility updates with [VisibilityDetectorController]
///
/// When this element is unmounted from the widget tree, it will:
/// 1. Nullify the [RenderSliverVisibilityDetector.onVisibilityChanged] callback
/// 2. Trigger disposal of the associated [RenderSliverVisibilityDetector]
/// 3. Release subscriptions to visibility tracking mechanisms
///
/// {@template sliver_visibility_detector_element_ownership}
/// This element is automatically created and managed by the framework when using
/// a [SliverVisibilityDetector] widget. Developers should never need to instantiate this
/// class directly.
/// {@endtemplate}
///
/// See also:
/// - [SliverVisibilityDetector], the widget that creates this element
/// - [RenderSliverVisibilityDetector], the render object managed by this element
class SliverVisibilityDetectorElement extends SingleChildRenderObjectElement {
SliverVisibilityDetectorElement(SliverVisibilityDetector widget)
: super(widget);

@override
void unmount() {
final renderObject = this.renderObject;
if (renderObject is RenderSliverVisibilityDetector) {
renderObject.onVisibilityChanged = null;
}
super.unmount();
}
}
1 change: 1 addition & 0 deletions packages/visibility_detector/lib/visibility_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

export 'src/visibility_detector.dart';
export 'src/visibility_detector_controller.dart';
export 'src/visibility_detector_element.dart';
Loading