constrainSizeAndAttemptToPreserveAspectRatio method
- Size size
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 is 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;
}
if (size.isEmpty) {
return constrain(size);
}
double width = size.width;
double height = size.height;
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;
}