rotatedTextSize function

Rect rotatedTextSize(
  1. Size size,
  2. int angle
)

This method returns the rect for given size and angle

Implementation

Rect rotatedTextSize(Size size, int angle) {
  final Rect rect = Rect.fromLTWH(0, 0, size.width, size.height);
  final vector.Matrix2 rotatorMatrix =
      vector.Matrix2.rotation(degreeToRadian(angle));

  final Rect movedToCenterAsOrigin = rect.shift(-rect.center);

  Offset topLeft = movedToCenterAsOrigin.topLeft;
  Offset topRight = movedToCenterAsOrigin.topRight;
  Offset bottomLeft = movedToCenterAsOrigin.bottomLeft;
  Offset bottomRight = movedToCenterAsOrigin.bottomRight;

  topLeft = transform(rotatorMatrix, topLeft);
  topRight = transform(rotatorMatrix, topRight);
  bottomLeft = transform(rotatorMatrix, bottomLeft);
  bottomRight = transform(rotatorMatrix, bottomRight);

  final List<Offset> rotOffsets = <Offset>[
    topLeft,
    topRight,
    bottomLeft,
    bottomRight
  ];

  final double minX =
      rotOffsets.map((Offset offset) => offset.dx).reduce(math.min);
  final double maxX =
      rotOffsets.map((Offset offset) => offset.dx).reduce(math.max);
  final double minY =
      rotOffsets.map((Offset offset) => offset.dy).reduce(math.min);
  final double maxY =
      rotOffsets.map((Offset offset) => offset.dy).reduce(math.max);

  final Rect rotateRect = Rect.fromPoints(
    Offset(minX, minY),
    Offset(maxX, maxY),
  );
  return rotateRect;
}