calculateImageFitSize function
Implementation
Size calculateImageFitSize(ui.Image image, Size contentSize) {
// 获取图像的宽高
final double imageWidth = image.width.toDouble();
final double imageHeight = image.height.toDouble();
// 获取内容区域的宽高
final double contentWidth = contentSize.width;
final double contentHeight = contentSize.height;
// 计算宽高比
final double imageAspectRatio = imageWidth / imageHeight;
final double contentAspectRatio = contentWidth / contentHeight;
double fitWidth;
double fitHeight;
if (imageAspectRatio > contentAspectRatio) {
// 图像宽高比大于内容宽高比,按宽度适配
fitWidth = contentWidth;
fitHeight = contentWidth / imageAspectRatio;
} else {
// 图像宽高比小于或等于内容宽高比,按高度适配
fitWidth = contentHeight * imageAspectRatio;
fitHeight = contentHeight;
}
// 如果图像的宽高小于内容区域的宽高,则放大到内容区域的宽高
if (fitWidth < contentWidth) {
fitWidth = contentWidth;
fitHeight = contentWidth / imageAspectRatio;
}
if (fitHeight < contentHeight) {
fitWidth = contentHeight * imageAspectRatio;
fitHeight = contentHeight;
}
return Size(fitWidth, fitHeight);
}