1

I need calculating the aspect ration from image from width and heigth

  • 3
    Possible 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
  • 2
    Possible 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 Answers1

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