Rectify

pub license: MIT build codecov

Dart extension methods to make the Rect class pleasant and convenient to work with.

MethodDescription
adjustAdjust by dx1, dy1, dx2 and dy2.
intersectsWhether two rectangles intersect.
moveBottomMoves along the bottom edge.
moveBottomLeftMoves along the bottom left corner.
moveBottomRightMoves along the bottom right corner.
moveCenterMoves along the center.
moveLeftMoves along the left edge.
moveRightMoves along the right edge.
moveTopMoves along the top edge.
moveTopLeftMoves along the top left corner.
moveTopRightMoves along the top right corner.
normalizeEnsures non-negative width and height.
setBottomSets the bottom edge.
setBottomLeftSets the bottom left corner.
setBottomRightSets the bottom right corner.
setHeightSets the height.
setLeftSets the left edge.
setRightSets the right edge.
setTopSets the top edge.
setTopLeftSets the top left corner.
setTopRightSets the top right corner.
setWidthSets the width.
normalizeExchanges width and height.
uniteBounding rectangle of two rectangles.

All above methods are extension methods and work on any object of type Rect.

Usage

To use this package, add rectify as a dependency in your pubspec.yaml file

import 'package:rectify/rectify.dart';

void main() {
  const ltrb1234 = Rect.fromLTRB(1, 2, 3, 4);
  const ltrb2345 = Rect.fromLTRB(2, 3, 4, 5);
  const ltrb5678 = Rect.fromLTRB(5, 6, 7, 8);
  const ltwh1234 = Rect.fromLTWH(1, 2, 3, 4);

  // adjust
  print(ltrb1234.adjust(1, 2, 3, 4)); // Rect.fromLTRB(2, 4, 6, 8)

  // intersection
  print(ltrb1234.intersects(ltrb2345)); // true
  print(ltrb1234.intersects(ltrb5678)); // false

  // move edge
  print(ltrb1234.moveLeft(5)); // Rect.fromLTRB(5, 2, 7, 4)
  print(ltrb1234.moveTop(5)); // Rect.fromLTRB(1, 5, 3, 7)
  print(ltrb1234.moveRight(5)); // Rect.fromLTRB(3, 2, 5, 4)
  print(ltrb1234.moveBottom(5)); // Rect.fromLTRB(1, 3, 3, 5)

  // move corner
  print(ltrb1234.moveTopLeft(Offset(5, 6))); // Rect.fromLTRB(5, 6, 7, 8)
  print(ltrb1234.moveTopRight(Offset(5, 6))); // Rect.fromLTRB(3, 6, 5, 8)
  print(ltrb1234.moveBottomLeft(Offset(5, 6))); // Rect.fromLTRB(5, 4, 7, 6)
  print(ltrb1234.moveBottomRight(Offset(5, 6))); // Rect.fromLTRB(3, 4, 5, 6)

  // move center
  print(ltrb1234.moveCenter(Offset(5, 6))); // Rect.fromLTRB(4, 5, 6, 7)

  // normalize
  print(Rect.fromLTWH(1, 2, -3, -4).normalize()); // Rect.fromLTWH(1, 2, 3, 4)

  // set edge
  print(ltrb1234.setBottom(5)); // Rect.fromLTRB(1, 2, 3, 5)
  print(ltrb1234.setLeft(5)); // Rect.fromLTRB(5, 2, 3, 4)
  print(ltrb1234.setRight(5)); // Rect.fromLTRB(1, 2, 5, 4)
  print(ltrb1234.setTop(5)); // Rect.fromLTRB(1, 5, 3, 4)

  // set corner
  print(ltrb1234.setTopLeft(Offset(5, 6))); // Rect.fromLTRB(5, 6, 3, 4)
  print(ltrb1234.setTopRight(Offset(5, 6))); // Rect.fromLTRB(1, 6, 5, 4)
  print(ltrb1234.setBottomLeft(Offset(5, 6))); // Rect.fromLTRB(5, 2, 3, 6)
  print(ltrb1234.setBottomRight(Offset(5, 6))); // Rect.fromLTRB(1, 2, 5, 6)

  // size
  print(ltwh1234.setSize(Size(5, 6))); // Rect.fromLTWH(1, 2, 5, 6))
  print(ltwh1234.setWidth(5)); // Rect.fromLTWH(1, 2, 5, 4))
  print(ltwh1234.setHeight(5)); // Rect.fromLTWH(1, 2, 3, 5))

  // transpose
  print(ltwh1234.transpose()); //Rect.fromLTWH(1, 2, 4, 3))

  // unite
  print(ltrb1234.unite(ltrb5678)); // Rect.fromLTRB(1, 2, 7, 8)
}

Libraries

rectify
Extension methods to make Rect pleasant and convenient to work with.