subtractCover function

Rect? subtractCover(
  1. Rect rect,
  2. Rect cover
)

Part of rect still visible when cover is painted over it, or null when it is fully hidden. Only a remainder that is itself a rect is trimmed (the cover spans the full height or width of rect); a corner overlap keeps the whole rect so no content is uncovered.

Implementation

Rect? subtractCover(Rect rect, Rect cover) {
  if (!rect.overlaps(cover)) {
    return rect;
  }
  if (cover.left <= rect.left &&
      cover.top <= rect.top &&
      cover.right >= rect.right &&
      cover.bottom >= rect.bottom) {
    return null;
  }
  if (cover.top <= rect.top && cover.bottom >= rect.bottom) {
    if (cover.left <= rect.left) {
      return Rect.fromLTRB(cover.right, rect.top, rect.right, rect.bottom);
    }
    if (cover.right >= rect.right) {
      return Rect.fromLTRB(rect.left, rect.top, cover.left, rect.bottom);
    }
  }
  if (cover.left <= rect.left && cover.right >= rect.right) {
    if (cover.top <= rect.top) {
      return Rect.fromLTRB(rect.left, cover.bottom, rect.right, rect.bottom);
    }
    if (cover.bottom >= rect.bottom) {
      return Rect.fromLTRB(rect.left, rect.top, rect.right, cover.top);
    }
  }
  return rect;
}