ensureVisible method
- RenderObject object, {
- double alignment = 0.0,
- Duration duration = Duration.zero,
- Curve curve = Curves.ease,
- ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicit,
- RenderObject? targetRenderObject,
Animates the position such that the given object is as visible as possible by just scrolling this position.
The optional targetRenderObject
parameter is used to determine which area
of that object should be as visible as possible. If targetRenderObject
is null, the entire RenderObject (as defined by its
RenderObject.paintBounds) will be as visible as possible. If
targetRenderObject
is provided, it must be a descendant of the object.
See also:
- ScrollPositionAlignmentPolicy for the way in which
alignment
is applied, and the way the givenobject
is aligned.
Implementation
Future<void> ensureVisible(
RenderObject object, {
double alignment = 0.0,
Duration duration = Duration.zero,
Curve curve = Curves.ease,
ScrollPositionAlignmentPolicy alignmentPolicy = ScrollPositionAlignmentPolicy.explicit,
RenderObject? targetRenderObject,
}) async {
assert(object.attached);
final RenderAbstractViewport? viewport = RenderAbstractViewport.maybeOf(object);
// If no viewport is found, return.
if (viewport == null) {
return;
}
Rect? targetRect;
if (targetRenderObject != null && targetRenderObject != object) {
targetRect = MatrixUtils.transformRect(
targetRenderObject.getTransformTo(object),
object.paintBounds.intersect(targetRenderObject.paintBounds),
);
}
double target;
switch (_applyAxisDirectionToAlignmentPolicy(alignmentPolicy)) {
case ScrollPositionAlignmentPolicy.explicit:
target = viewport.getOffsetToReveal(
object,
alignment,
rect: targetRect,
axis: axis,
).offset;
target = clampDouble(target, minScrollExtent, maxScrollExtent);
case ScrollPositionAlignmentPolicy.keepVisibleAtEnd:
target = viewport.getOffsetToReveal(
object,
1.0, // Aligns to end
rect: targetRect,
axis: axis,
).offset;
target = clampDouble(target, minScrollExtent, maxScrollExtent);
if (target < pixels) {
target = pixels;
}
case ScrollPositionAlignmentPolicy.keepVisibleAtStart:
target = viewport.getOffsetToReveal(
object,
0.0, // Aligns to start
rect: targetRect,
axis: axis,
).offset;
target = clampDouble(target, minScrollExtent, maxScrollExtent);
if (target > pixels) {
target = pixels;
}
}
if (target == pixels) {
return;
}
if (duration == Duration.zero) {
jumpTo(target);
return;
}
return animateTo(target, duration: duration, curve: curve);
}