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 oldViewportDimensions =
      hasViewportDimension ? this.viewportDimension : null;

  if (viewportDimension == oldViewportDimensions) {
    return true;
  }

  final result = super.applyViewportDimension(viewportDimension);
  final oldPixels = hasPixels ? pixels : null;

  // TODO: Cache x when viewport resizes to zero? (See PageView)
  final x = initialX;
  final newPixels = x != null ? getPixelsFromX(x) : null;

  if (newPixels != null && newPixels != oldPixels) {
    correctPixels(newPixels);
    return false;
  }

  return result;
}