fitSize function

Size fitSize(
  1. Size containerSize,
  2. Size imgSize, [
  3. bool fill = false
])

Implementation

Size fitSize(Size containerSize, Size imgSize, [bool fill = false]) {
  double containerWHScale = containerSize.width / containerSize.height;
  double imgWHScale = imgSize.width / imgSize.height;
  double scale;
  if (imgWHScale < containerWHScale) {
    if (fill) {
      scale = containerSize.width / imgSize.width;
    } else {
      scale = containerSize.height / imgSize.height;
    }
  } else {
    if (fill) {
      scale = containerSize.height / imgSize.height;
    } else {
      scale = containerSize.width / imgSize.width;
    }
  }
  final height = imgSize.height * scale;
  final width = imgSize.width * scale;
  return Size(width, height);
}