setAspectRatio method

void setAspectRatio(
  1. double? ratio
)

Resize the crop rectangle to the given aspect ratio (width / height), or pass null to free the crop and reset to the full image.

The crop stays centered and is scaled to fit inside the current image bounds.

Implementation

void setAspectRatio(double? ratio) {
  if (ratio == null) {
    lockedAspectRatio = null;
    crop = Offset.zero & imageSize;
  } else {
    lockedAspectRatio = 1 / ratio;
    final imgRatio = imageSize.width / imageSize.height;
    final w = ratio >= imgRatio ? imageSize.width : imageSize.height * ratio;
    final h = ratio >= imgRatio ? imageSize.width / ratio : imageSize.height;
    crop = Rect.fromCenter(
      center: Offset(imageSize.width / 2, imageSize.height / 2),
      width: w,
      height: h,
    );
  }
  // Telegram's `setLockedAspectRatio performResize:` runs the zoom
  // and area springs in parallel — the settle covers the transform
  // half, the immediate confirm tick lets the screen refit the
  // camera alongside it.
  _hideBackdrop(animated: false);
  settleToCoverage().then((_) {
    if (!disposed) _showBackdrop(animated: false);
  }).unawaited();
  cropChangedSinceConfirm = false;
  confirmTick.value++;
  notifyListeners();
}