shiftWithinBounds static method

Rect shiftWithinBounds({
  1. required Rect rect,
  2. required Rect bounds,
})

A utility for calculating a new Rect from rect such that rect is fully constrained within bounds.

Any point in the output rect is guaranteed to also be a point contained in bounds.

It is a runtime error for rect.width to be greater than bounds.width, and it is also an error for rect.height to be greater than bounds.height.

This algorithm translates rect the shortest distance such that it is entirely within bounds.

If rect is already within bounds, no shift will be applied to rect and rect will be returned as-is.

It is perfectly valid for the output rect to have a point along the edge of the bounds. If the desired output rect requires that no edges are parallel to edges of bounds, see Rect.deflate by 1 on bounds to achieve this effect.

Implementation

static Rect shiftWithinBounds({
  required Rect rect,
  required Rect bounds,
}) {
  assert(rect.width <= bounds.width,
      'attempted to shift $rect within $bounds, but the rect has a greater width.');
  assert(rect.height <= bounds.height,
      'attempted to shift $rect within $bounds, but the rect has a greater height.');

  Offset rectShift = Offset.zero;
  if (rect.left < bounds.left) {
    rectShift += Offset(bounds.left - rect.left, 0);
  } else if (rect.right > bounds.right) {
    rectShift += Offset(bounds.right - rect.right, 0);
  }

  if (rect.top < bounds.top) {
    rectShift += Offset(0, bounds.top - rect.top);
  } else if (rect.bottom > bounds.bottom) {
    rectShift += Offset(0, bounds.bottom - rect.bottom);
  }

  return rect.shift(rectShift);
}