How to add/manage multiple image size/resolution in flutter assets

11.1k Views Asked by At

Kindly help with the simplest/shortest way

wish to manage multiple images in flutter like hdpi/mdpi... in Android studio

How do I include image assets for Flutter? What about multiple resolutions?

1

There are 1 best solutions below

0
On BEST ANSWER

Declaring resolution aware image assets Source - https://flutter.dev/docs/development/ui/assets-and-images#resolution-aware

AssetImage understands how to map a logical requested asset onto one that most closely matches the current device pixel ratio. In order for this mapping to work, assets should be arranged according to a particular directory structure:

content_copy
  .../image.png
  .../Mx/image.png
  .../Nx/image.png
  ...etc.

…where M and N are numeric identifiers that correspond to the nominal resolution of the images contained within, in other words, they specify the device pixel ratio that the images are intended for.

The main asset is assumed to correspond to a resolution of 1.0. For example, consider the following asset layout for an image named my_icon.png:

content_copy
  .../my_icon.png
  .../2.0x/my_icon.png
  .../3.0x/my_icon.png

On devices with a device pixel ratio of 1.8, the asset .../2.0x/my_icon.png would be chosen. For a device pixel ratio of 2.7, the asset .../3.0x/my_icon.png would be chosen.

If the width and height of the rendered image are not specified on the Image widget, the nominal resolution is used to scale the asset so that it will occupy the same amount of screen space as the main asset would have, just with a higher resolution. That is, if .../my_icon.png is 72px by 72px, then .../3.0x/my_icon.png should be 216px by 216px; but they both will render into 72px by 72px (in logical pixels) if width and height are not specified.

Each entry in the asset section of the pubspec.yaml should correspond to a real file, with the exception of the main asset entry. If the main asset entry does not correspond to a real file, then the asset with the lowest resolution will be used as the fallback for devices with device pixel ratios below that resolution. The entry should still be included in the pubspec.yaml manifest, however.