I need calculating the aspect ration from image from width and heigth
Asked
Active
Viewed 9,554 times
1
-
3Possible duplicate of [How do I determine the width and height of an image in Flutter?](https://stackoverflow.com/questions/44665955/how-do-i-determine-the-width-and-height-of-an-image-in-flutter) – Sub 6 Resources Sep 11 '19 at 13:48
-
2Possible duplicate of [Change the primary image dimension without touching placeholder image size in Flutter](https://stackoverflow.com/questions/54522290/change-the-primary-image-dimension-without-touching-placeholder-image-size-in-fl) – Amit Prajapati Sep 11 '19 at 13:54
1 Answers
5
If I understood you correctly, you need size of widget.
For this you have to use GlobalKey
:
final _key = GlobalKey();
Then set the key
when you create widget:
Image.asset('asset_image', key: _key,); //it can be any widget
And after you can get size of this widget:
Size size = _key.currentContext.size;
double height = _key.currentContext.size.height;
double width = _key.currentContext.size.width;
double aspectRatio = _key.currentContext.size.aspectRatio;
UPD for NetworkImage
:
NetworkImage
isn't a widget, it can be used in Image
, so you can get aspect ratio as in example above:
Image(image: NetworkImage('url'), key: _key,);
Or you can get width and height from listener and calculate ratio:
Image(image: NetworkImage('url'))
.image
.resolve(ImageConfiguration())
.addListener((ImageInfo info, bool _) {
int width = info.image.width;
int height = info.image.height;
});

Dmitry Garazhny
- 344
- 3
- 12
-
I want get size of image from NetworkImage and calculate the aspect ratio – ANTONIO VENANCIO Sep 12 '19 at 19:02
-
@ANTONIOVENANCIO you can get the aspect ratio by doing following: width/height. Your welcome – Christian X Aug 19 '20 at 21:07