fitContain method

FittedBox fitContain({
  1. AlignmentGeometry alignment = Alignment.center,
  2. Clip clipBehavior = Clip.none,
})

Wraps the widget in a FittedBox with BoxFit.contain strategy.

Scales the widget to fit entirely within the available space while maintaining its aspect ratio. The widget will be as large as possible without being cropped, but may leave empty space if the aspect ratios don't match.

This is the most commonly used fitting strategy as it preserves the entire content while scaling appropriately.

Parameters:

  • alignment: How to align the child within the fitted box. Defaults to Alignment.center.
  • clipBehavior: How to clip the child if it overflows. Defaults to Clip.none.

Returns a FittedBox widget with contain fitting.

Example:

Image.asset('large_image.jpg')
  .fitContain() // Scales image to fit without cropping
  .sizedBox(width: 200, height: 100);

Implementation

FittedBox fitContain({
  AlignmentGeometry alignment = Alignment.center,
  Clip clipBehavior = Clip.none,
}) =>
    fittedBox(
      fit: BoxFit.contain,
      alignment: alignment,
      clipBehavior: clipBehavior,
    );