resizeImage function

Size resizeImage({
  1. required Size start,
  2. required Offset delta,
  3. required ImageHandle handle,
  4. ImageSizeLimits limits = const ImageSizeLimits(),
  5. double? aspectRatio,
})

The size a drag of delta on handle produces, starting from start.

With an aspectRatio the corner handles keep the image's shape and the larger of the two movements wins, which is what makes a diagonal drag feel like it follows the pointer rather than only its horizontal part. Edge handles resize one axis and derive the other, so an image never distorts by dragging its side either.

Implementation

Size resizeImage({
  required Size start,
  required Offset delta,
  required ImageHandle handle,
  ImageSizeLimits limits = const ImageSizeLimits(),
  double? aspectRatio,
}) {
  if (aspectRatio == null || aspectRatio <= 0) {
    return limits.clamp(
      Size(
        start.width + delta.dx * handle.horizontal,
        start.height + delta.dy * handle.vertical,
      ),
    );
  }

  final byWidth = start.width + delta.dx * handle.horizontal;
  final byHeight = start.height + delta.dy * handle.vertical;
  final double width;
  if (!handle.isCorner) {
    width = handle.horizontal != 0 ? byWidth : byHeight * aspectRatio;
  } else {
    // Whichever axis the pointer moved further along is the one it meant.
    final horizontal = (delta.dx * handle.horizontal).abs();
    final vertical = (delta.dy * handle.vertical).abs();
    width = horizontal >= vertical ? byWidth : byHeight * aspectRatio;
  }
  return limits.clampScaled(Size(width, width / aspectRatio), aspectRatio);
}