constrainSizeAndAttemptToPreserveAspectRatio method
Returns a size that attempts to meet the following conditions, in order:
- The size must satisfy these constraints.
- The aspect ratio of the returned size matches the aspect ratio of the given size.
- The returned size as big as possible while still being equal to or smaller than the given size.
Implementation
Size constrainSizeAndAttemptToPreserveAspectRatio(Size size) {
if (isTight) {
Size result = smallest;
assert(() { result = _debugPropagateDebugSize(size, result); return true; }());
return result;
}
double width = size.width;
double height = size.height;
assert(width > 0.0);
assert(height > 0.0);
final double aspectRatio = width / height;
if (width > maxWidth) {
width = maxWidth;
height = width / aspectRatio;
}
if (height > maxHeight) {
height = maxHeight;
width = height * aspectRatio;
}
if (width < minWidth) {
width = minWidth;
height = width / aspectRatio;
}
if (height < minHeight) {
height = minHeight;
width = height * aspectRatio;
}
Size result = Size(constrainWidth(width), constrainHeight(height));
assert(() { result = _debugPropagateDebugSize(size, result); return true; }());
return result;
}