Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with different icon sizes

Tags:

flutter

When developing for iphone, in Xcode/Swift there is an asset file where you add all the icons your application will use with 1, 2 and 3x sizes. There is probably a similar thing with Android that I don't know yet.

When developing for Flutter:

  • How do you deal with the different screen resolutions to use the right icon sizes?
  • If similar to Xcode/Swift, where do you put those files and how do you get them? What about Android?

If you know a tutorial or web page, just let me know! The only ones that I found are only dedicated for app icon and launch screen.

Thanks

like image 739
David Avatar asked Oct 26 '25 07:10

David


2 Answers

Just check the official documentation about resolution-aware-image. This is based on the device pixel ratio.

Here you have an example about the structure of your files.

  .../pubspec.yaml
  .../icons/heart.png
  .../icons/1.5x/heart.png
  .../icons/2.0x/heart.png

And this is how you have to declare the assets files into your pubspec.file

        flutter:
          assets:
            - icons/heart.png

This is the link: https://flutter.io/docs/development/ui/assets-and-images#declaring-resolution-aware-image-assets

like image 184
diegoveloper Avatar answered Oct 28 '25 22:10

diegoveloper


To add to @diegoveloper answer.

Some plugins like google-maps do not honour this standard and require you to call BitmapDescriptor.fromAsset('icons/heart.png') which is not converted to the correct image. In this case you can get the correct image from AssetImage by first createLocalImageConfiguration using the BuildContext as defined here. Then use this configuration to resolve the correct image as follows:

ImageConfiguration config = createLocalImageConfiguration(context);
AssetImage('icons/heart.png')
   .obtainKey(config)
   .then((resolvedImage) {
       print('Name: ' + resolvedImage.onValue.name);
    });

In summary

Create an asset folder with the naming convention:

pathtoimages/image.png
pathtoimages/Mx/image.png
pathtoimages/Nx/image.png
pathtoimages/etc.

Where M and N are resolutions (2.0x) or themes (dark). Then add the image or all the images to the pubspec.file as either

flutter:
  assets:
    - pathtoimages/image.png

or

flutter:
  assets:
    - pathtoimages/

Load the images in using either AssetImage('pathtoimages/image.png') or resolving to the actual file name as mentioned above, depending on the library being used.

like image 29
Jason Avatar answered Oct 28 '25 21:10

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!