How do I determine the width and height of an image in Flutter?

后端 未结 5 1559
傲寒
傲寒 2020-11-27 15:00

Assume I have declared my image in my pubspec.yaml like this:

  assets:
    - assets/kitten.jpg

And my Flutter code is this:



        
5条回答
  •  暖寄归人
    2020-11-27 15:41

    The other answers seem overly complicated if you just want the width and height of an image in an async function. You can get the image resolution using flutter lib directly like this:

    import 'dart:io';
    
    File image = new File('image.png'); // Or any other way to get a File instance.
    var decodedImage = await decodeImageFromList(image.readAsBytesSync());
    print(decodedImage.width);
    print(decodedImage.height);
    

提交回复
热议问题