Sub topic 8
Displaying Images with the Image Widget
This chapter assumes you know basic Dart syntax, Flutter widgets, StatelessWidget, the build method, and pubspec.yaml. It focuses on how the Image widget turns image data into pixels in a Flutter layout.
What the Image widget does
Image is a widget that loads an image, decodes it into pixels, and paints those pixels in its allotted space. Flutter provides constructors for different sources, including:
Image.assetfor images packaged with the applicationImage.networkfor images downloaded from a URLImage.filefor images stored on a deviceImage.memoryfor encoded image bytes in memory
The constructor chooses where the image comes from. The widget's layout properties decide how it appears.
import 'package:flutter/material.dart';
void main() {
runApp(const ImageExampleApp());
}
class ImageExampleApp extends StatelessWidget {
const ImageExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Image example')),
body: Center(
child: SizedBox(
width: 300,
height: 180,
child: Image.network(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg',
fit: BoxFit.cover,
semanticLabel: 'An owl sitting on a branch',
),
),
),
),
);
}
}width and height give the image layout dimensions. They do not change the source file itself. The fit property controls how the source rectangle is placed inside those dimensions.
Understanding BoxFit
BoxFit describes the relationship between the image's original aspect ratio and its destination box.
| BoxFit value | Behavior |
|---|---|
| `contain` | Shows the complete image, possibly leaving empty space |
| `cover` | Fills the box, cropping parts that do not fit |
| `fill` | Stretches the image to the box, which can distort it |
| `none` | Keeps the original size and does not scale |
contain is useful when every part of an image must remain visible, such as a product diagram. cover works well for thumbnails and banners where a consistent shape matters more than showing every edge.
Flutter first obtains the image data, then decodes it, lays out the widget, and finally paints the pixels.
Constraints and aspect ratio
An image does not always choose its own size. Parent widgets provide constraints. For example, a SizedBox gives the image a fixed rectangle, while a Row may provide limited horizontal space.
Avoid assuming that width and height alone guarantee a visible image. A parent can impose tighter constraints. Use layout widgets such as SizedBox, AspectRatio, or Expanded when the image must fit a particular design.
Choosing a source
Use an asset when the image ships with the app and must work offline. Use a network image when a server supplies changing content. Use a file or memory image when another part of the application already owns local image data.
For production interfaces, plan the image's dimensions and aspect ratio before choosing BoxFit. This prevents unexpected cropping, stretching, or layout changes while content loads.
Exercise
Which BoxFit should you usually choose for a fixed-size thumbnail when the thumbnail must fill its box and cropping is acceptable?
Show answer
Choose BoxFit.cover. It fills the destination box while preserving the image's aspect ratio, cropping any excess edges.