SizeAspectRatio.resize constructor

SizeAspectRatio.resize(
  1. int width,
  2. int height,
  3. int maxWidth,
  4. int maxHeight,
)

Implementation

factory SizeAspectRatio.resize(
  int width,
  int height,
  int maxWidth,
  int maxHeight,
) {
  double scaleFactor = (maxWidth / width > maxHeight / height)
      ? maxHeight / height
      : maxWidth / width;
  int newWidth = (width * scaleFactor).round();
  int newHeight = (height * scaleFactor).round();
  int finalWidth = newWidth.clamp(0, maxWidth);
  int finalHeight = newHeight.clamp(0, maxHeight);
  return SizeAspectRatio(
    width: finalWidth,
    height: finalHeight,
  );
}