getScaledSize method

Size getScaledSize(
  1. double originalWidth,
  2. double originalHeight,
  3. RenderImage renderImage,
  4. BoxFit fit,
)

Implementation

Size getScaledSize(
  double originalWidth,
  double originalHeight,
  RenderImage renderImage,
  BoxFit fit,
) {
  final double targetWidth = renderImage.size.width;
  final double targetHeight = renderImage.size.height;

  final double aspectRatio = originalWidth / originalHeight;
  switch (fit) {
    case BoxFit.fill:
      return Size(targetWidth, targetHeight);
    case BoxFit.contain:
      if (targetWidth / aspectRatio <= targetHeight) {
        return Size(targetWidth, targetWidth / aspectRatio);
      }
      return Size(targetHeight * aspectRatio, targetHeight);
    case BoxFit.cover:
      if (targetWidth / aspectRatio >= targetHeight) {
        return Size(targetWidth, targetWidth / aspectRatio);
      }
      return Size(targetHeight * aspectRatio, targetHeight);
    case BoxFit.fitWidth:
      return Size(targetWidth, targetWidth / aspectRatio);
    case BoxFit.fitHeight:
      return Size(targetHeight * aspectRatio, targetHeight);
    case BoxFit.none:
      return Size(originalWidth, originalHeight);
    case BoxFit.scaleDown:
      if (originalWidth > targetWidth || originalHeight > targetHeight) {
        return getScaledSize(
          originalWidth,
          originalHeight,
          renderImage,
          BoxFit.contain,
        );
      }
      return Size(originalWidth, originalHeight);
  }
}