applyViewportDimension method

  1. @override
bool applyViewportDimension(
  1. double viewportDimension
)
override

Called when the viewport's extents are established.

The argument is the dimension of the RenderViewport in the main axis (e.g. the height, for a vertical viewport).

This may be called redundantly, with the same value, each frame. This is called during layout for the RenderViewport. If the viewport is configured to shrink-wrap its contents, it may be called several times, since the layout is repeated each time the scroll offset is corrected.

If this is called, it is called before applyContentDimensions. If this is called, applyContentDimensions will be called soon afterwards in the same layout phase. If the viewport is not configured to shrink-wrap its contents, then this will only be called when the viewport recomputes its size (i.e. when its parent lays out), and not during normal scrolling.

If applying the viewport dimensions changes the scroll offset, return false. Otherwise, return true. If you return false, the RenderViewport will be laid out again with the new scroll offset. This is expensive. (The return value is answering the question "did you accept these viewport dimensions unconditionally?"; if the new dimensions change the ViewportOffset's actual pixels value, then the viewport will need to be laid out again.)

Implementation

@override
bool applyViewportDimension(double viewportDimension) {
  final double? oldViewportDimensions =
      // fix viewportDimension
      hasViewportDimension ? this.viewportDimension - pageSpacing : null;

  if (viewportDimension == oldViewportDimensions) {
    return true;
  }
  final bool result = super.applyViewportDimension(viewportDimension);
  final double? oldPixels = hasPixels ? pixels : null;
  double page;
  if (oldPixels == null) {
    page = _pageToUseOnStartup;
  } else if (oldViewportDimensions == 0.0) {
    // If resize from zero, we should use the _cachedPage to recover the state.
    page = _cachedPage!;
  } else {
    page = getPageFromPixels(oldPixels, oldViewportDimensions!);
  }
  final double newPixels = getPixelsFromPage(page);

  // If the viewportDimension is zero, cache the page
  // in case the viewport is resized to be non-zero.
  _cachedPage = (viewportDimension == 0.0) ? page : null;
  if (newPixels != oldPixels) {
    correctPixels(newPixels);
    return false;
  }
  return result;
}