layoutFit method
void
layoutFit(})
Lays out, positions, and scales the child so that it fits in rect
provided a fit
and alignment
.
- BoxFit, the enum with each possible fit type.
- FittedBox, a widget that has similar behavior.
- layout, which lays out the child given raw BoxConstraints.
- layoutRect, which positions the child so that it fits in a rect.
Implementation
void layoutFit(
Rect rect, {
BoxFit fit = BoxFit.contain,
Alignment alignment = Alignment.center,
}) {
final childSize = layout(const BoxConstraints());
final sizes = applyBoxFit(fit, childSize, rect.size);
final scaleX = sizes.destination.width / sizes.source.width;
final scaleY = sizes.destination.height / sizes.source.height;
final sourceRect =
alignment.inscribe(sizes.source, Offset.zero & childSize);
final destinationRect = alignment.inscribe(sizes.destination, rect);
setTransform(
Matrix4.translationValues(destinationRect.left, destinationRect.top, 0.0)
..scale(scaleX, scaleY, 1.0)
..translate(-sourceRect.left, -sourceRect.top),
);
}