Sub topic 11
Sizing and Cropping Images with BoxFit
Assume you know basic Flutter widgets, StatelessWidget, the build method, and how to declare image assets. This chapter focuses on how Flutter fits an image into the space given by its parent.
How image sizing works
An image has an intrinsic size, which is the size described by its pixels. A widget around it can impose different constraints, such as a width of 300 logical pixels and a height of 180 logical pixels.
Image uses fit to decide how the source image should occupy those constraints. The BoxFit value does not change the source image. It changes how Flutter scales and positions the image inside its layout box.
import 'package:flutter/material.dart';
class ProductImage extends StatelessWidget {
const ProductImage({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: Image.asset(
'assets/images/product.png',
width: 300,
height: 180,
fit: BoxFit.cover,
semanticLabel: 'A product on a wooden table',
),
);
}
}The width and height create a 300-by-180 box. BoxFit.cover scales the image until the entire box is filled. If the image and box have different aspect ratios, some of the image is cropped.
An aspect ratio compares width with height. A landscape image may have a ratio of 16:9, while a square box has a ratio of 1:1. Fitting one ratio into another always requires either empty space or cropping, unless the image is distorted.
Choosing a BoxFit value
| Value | Behavior | Common use |
|---|---|---|
| `cover` | Fills the box and crops excess image | Banners, avatars, card headers |
| `contain` | Shows the complete image and leaves empty space | Logos, product previews |
| `fill` | Fills the box, possibly distorting the image | Rare cases where distortion is acceptable |
| `fitWidth` | Matches the box width and may overflow vertically | Full-width content images |
| `fitHeight` | Matches the box height and may overflow horizontally | Fixed-height galleries |
| `none` | Uses the image's natural size | Pixel-oriented content |
| `scaleDown` | Uses `none` or `contain`, whichever keeps the image smaller | Preventing enlargement |
For cover, the crop position defaults to the center. Change it with alignment.
ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Image.asset(
'assets/images/portrait.png',
width: 240,
height: 160,
fit: BoxFit.cover,
alignment: Alignment.topCenter,
),
)This keeps the top of the portrait visible and crops more from the bottom. ClipRRect clips the painted image to rounded corners. Without clipping, the image itself can still paint square corners.
Constraints and common errors
BoxFit only works inside a finite layout box. If a parent gives an image unbounded height, such as some Column or scrolling layouts, setting fit alone does not create a useful height. Give the image dimensions directly, use AspectRatio, or place it in a constrained parent.
BoxFit.cover can remove important content. Test it with the longest translations, different screen sizes, and images whose subjects are near an edge. Use contain when every pixel must remain visible.
Exercise
A 400-by-200 image must appear inside a 200-by-200 avatar box. Which fit shows the whole image, and which fit fills the avatar while cropping the sides?
Show answer
BoxFit.contain shows the whole image with empty space above and below. BoxFit.cover fills the square and crops the left and right sides.