motionEnvelope function

Rect motionEnvelope(
  1. Rect previous,
  2. Rect current
)

Area to mask while a widget is moving from previous to current.

Rects are published after layout and cross the platform channel asynchronously, while native captures whatever frame is on screen. During a scroll or a page transition the frame on screen can be one ahead of or one behind the rect native holds, which leaves a strip of the content uncovered. Masking the previous, current and next (extrapolated) position covers both directions. Once the widget stops, callers publish the exact rect again.

The extrapolation uses 1.5x the last displacement so that transitions and flings that are still accelerating stay covered.

Implementation

Rect motionEnvelope(Rect previous, Rect current) {
  const double lookAhead = 1.5;
  final Rect predicted = Rect.fromLTRB(
    current.left + (current.left - previous.left) * lookAhead,
    current.top + (current.top - previous.top) * lookAhead,
    current.right + (current.right - previous.right) * lookAhead,
    current.bottom + (current.bottom - previous.bottom) * lookAhead,
  );
  return previous.expandToInclude(current).expandToInclude(predicted);
}