Sub topic 4
Resolution-aware Images with 2.0x and 3.0x Folders
Prerequisites
This chapter assumes that you know basic Dart syntax, can build a simple Flutter widget tree, and can edit pubspec.yaml. You should also know that YAML uses indentation to describe structure.
A resolution-aware image is an image for which Flutter provides several files at different pixel densities. The app refers to one logical asset path, while Flutter chooses the file that best matches the device screen.
Why one image is not enough
A screen has a device pixel ratio (DPR). The DPR describes how many physical pixels represent one logical pixel.
For example, a device might use two physical pixels for each logical pixel. Its DPR is then 2.0. A 100-logical-pixel image needs about 200 physical pixels in each dimension to look sharp on that screen.
If you use only a small image, Flutter must enlarge it. Enlarging can make edges and text inside the image look blurry. If you use only a very large image on every device, low-density devices download and decode more data than necessary.
Flutter solves this with image variants:
| Variant | Typical use | Image size for a 100-logical-pixel display |
|---|---|---|
| `1.0x` base image | Low-density screens | 100 physical pixels |
| `2.0x` image | High-density screens | 200 physical pixels |
| `3.0x` image | Very high-density screens | 300 physical pixels |
The image still occupies about 100 logical pixels in your layout. Only the source file changes.
Organizing the files
Place the base image at the logical asset path. Place higher-density versions in directories named 2.0x and 3.0x below that path.
For example:
assets/images/logo.png
assets/images/2.0x/logo.png
assets/images/3.0x/logo.pngThe files should show the same content and have the same aspect ratio. Their pixel dimensions should scale with their directory name:
logo.png: 120 × 120 physical pixels2.0x/logo.png: 240 × 240 physical pixels3.0x/logo.png: 360 × 360 physical pixels
Flutter treats the base file as the 1.0x variant. The 2.0x and 3.0x directory names tell Flutter the intended scale.
Declare the base directory in pubspec.yaml:
flutter:
assets:
- assets/images/The declaration includes the files below the directory, including the resolution variants. You normally do not declare each variant separately.
How Flutter selects a variant
Your widget refers to the base path:
import 'package:flutter/material.dart';
class Logo extends StatelessWidget {
const Logo({super.key});
@override
Widget build(BuildContext context) {
return Image.asset(
'assets/images/logo.png',
width: 120,
height: 120,
);
}
}Flutter reads the device pixel ratio and selects the closest available variant. Your Dart code does not change between a low-density phone, a high-density phone, and a tablet.
The process looks like this:
Flutter also uses the selected variant's scale when it calculates the image's logical size. Therefore, a 240-pixel-wide image in the 2.0x directory represents about 120 logical pixels, not 240 logical pixels.
Choosing dimensions and quality
Start with the logical size the image needs in the interface. Multiply that size by each scale.
For an image displayed at 80 logical pixels:
- The base image should be about 80 pixels wide.
- The
2.0ximage should be about 160 pixels wide. - The
3.0ximage should be about 240 pixels wide.
You can provide only the variants that your app needs. If a device falls between the available scales, Flutter chooses the closest available option. Providing a 2.0x image but no 3.0x image is valid, although the image may be enlarged slightly on a 3.0-DPR device.
Do not put a differently cropped or differently composed image in each folder. A user should see the same logo or illustration regardless of the selected density. Use the variants only to change pixel resolution.
A source image that contains tiny text or thin lines needs extra care. Scaling cannot restore detail that was absent from the original file. Export each variant from a high-quality source rather than repeatedly enlarging a smaller export.
Common mistakes
Referring to a variant directory in code
Use:
Image.asset('assets/images/logo.png')Do not use this as the normal application path:
Image.asset('assets/images/2.0x/logo.png')The second form bypasses the resolution-selection mechanism and may load the wrong scale on another device.
Declaring only a variant
If pubspec.yaml lists only assets/images/2.0x/, the base logical asset path is not properly established for normal variant selection. Declare the parent directory or the base file.
Using inconsistent names
Flutter expects corresponding files to have matching relative paths. These names describe variants of one asset:
assets/images/logo.png
assets/images/2.0x/logo.png
assets/images/3.0x/logo.pngThese names describe different assets:
assets/images/logo.png
assets/images/2.0x/logo-large.pngExpecting variants to reduce the installed package size
All declared variants are bundled into the application. Resolution selection reduces the decoded image size used at runtime, but it does not automatically remove unused variants from the installed app.
After adding or renaming assets, run flutter pub get and perform a full restart if the running app does not recognize the changes. A hot reload does not always refresh asset metadata.
Exercise
You display an icon at 48 logical pixels. Which files would you create for the base, 2.0x, and 3.0x variants, and which path should Image.asset use?
Show answer
Create files of about 48, 96, and 144 physical pixels respectively at assets/images/icon.png, assets/images/2.0x/icon.png, and assets/images/3.0x/icon.png. Use Image.asset('assets/images/icon.png') so Flutter can select the appropriate variant.