getRenderedVideoContentBounds function
Calculates the actual rendered video area inside a container, accounting for letterboxing (object-fit: contain equivalent).
Implementation
VideoContentBounds getRenderedVideoContentBounds({
required double containerWidth,
required double containerHeight,
required double videoWidth,
required double videoHeight,
}) {
if (containerWidth <= 0 ||
containerHeight <= 0 ||
videoWidth <= 0 ||
videoHeight <= 0) {
return VideoContentBounds(
left: 0, top: 0, width: containerWidth, height: containerHeight);
}
final containerAspect = containerWidth / containerHeight;
final videoAspect = videoWidth / videoHeight;
double renderWidth, renderHeight, offsetX, offsetY;
if (containerAspect > videoAspect) {
// Black bars on left and right
renderHeight = containerHeight;
renderWidth = containerHeight * videoAspect;
offsetX = (containerWidth - renderWidth) / 2;
offsetY = 0;
} else {
// Black bars on top and bottom
renderWidth = containerWidth;
renderHeight = containerWidth / videoAspect;
offsetX = 0;
offsetY = (containerHeight - renderHeight) / 2;
}
return VideoContentBounds(
left: offsetX,
top: offsetY,
width: renderWidth,
height: renderHeight,
);
}