FlutterAdvanced 2 min read

Flutter image decode sizing: logical pixels and image memory

Choose an image decode resolution that matches its display size and device pixel ratio.

On this page

An image displayed at 160 logical pixels wide does not always need its full source resolution in memory. Decoding a large photograph for every small list thumbnail can waste memory even when the original file was compressed efficiently.

Separate the three sizes

The downloaded file has a byte size. Its decoded image has pixel dimensions. The widget receives logical layout constraints. Compression, decode resolution and on-screen sizing solve different parts of the problem.

ResizeImage can request a smaller decoded image. Its current useLogicalSize option interprets the supplied dimensions through the device pixel ratio. The default remains physical pixel sizing for compatibility. This matters when the same app runs on displays with different densities.

Add one test asset

Create assets/images/lesson-cover.jpg in a local project and register that directory in pubspec.yaml. Use a photograph you own or have permission to distribute. Then place this fragment inside a widget’s build method:

Image(
  image: const ResizeImage(
    AssetImage('assets/images/lesson-cover.jpg'),
    width: 160,
    useLogicalSize: true,
  ),
  width: 160,
  height: 100,
  fit: BoxFit.cover,
)

The first width is the decode request. The Image width and height choose the display box. BoxFit.cover controls how the decoded picture fits that box; it can crop the picture without modifying the source asset.

Reason about density

At a device pixel ratio of 3, a 160-logical-pixel request targets 480 physical pixels across. That is a resolution calculation, not a promise about file bytes or exact memory use. A source smaller than the target cannot gain real detail by being enlarged.

Compare the result with useLogicalSize: false, particularly around fine text or sharp edges in a thumbnail. Read the ResizeImage constructor when selecting dimensions or an alternate resize policy.

Verify a list, not only a single image

Use profile mode and scroll a realistic image-heavy list. Record decoded image dimensions, memory and visible quality. An excessively small decode can save memory but produce blur; an excessively large decode can keep unused pixels in memory.

Cache keys include the image provider configuration. Repeatedly generating many near-identical requested sizes can create avoidable cache variants. Prefer a small set of meaningful thumbnail sizes from your layout rather than a new arbitrary value on each frame.

Exercise

Prepare 80-, 160- and 320-logical-pixel thumbnail variants and compare them at two device pixel ratios. Write down which sizes you would use for a list and a detail page. This local asset example does not require a network request or DartPad access.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.