getScrollOffset method
Returns the current scroll offset as an Offset
.
This method determines the scroll offset by checking if the scrollable
widget is managed by a parent or has its own ScrollController
.
-
If a
ScrollController
is provided, it uses the controller to fetch the scroll position. -
If no controller is provided, it attempts to retrieve the scroll position from the parent scrollable widget using the context.
-
reverse
: Whether to reverse the direction of the offset.
Returns an Offset
representing the scroll position or Offset.zero
if
no position is available.
Implementation
Offset getScrollOffset({required bool reverse}) {
var scrollPosition = Scrollable.maybeOf(_context)?.position;
final scrollController = _scrollController;
// Use the provided ScrollController's position if it has clients.
if (scrollController != null && scrollController.hasClients) {
scrollPosition = scrollController.position;
}
if (scrollPosition != null) {
final pixels = scrollPosition.pixels;
final isScrollingVertical = scrollPosition.axis == Axis.vertical;
final offset = Offset(
isScrollingVertical ? 0.0 : pixels,
isScrollingVertical ? pixels : 0.0,
);
return reverse ? -offset : offset;
}
return Offset.zero;
}