6

Is there a way to get the height of a CachedNetworkImage. I found this solution to get the height of a regular network image. Is there a way to implement this for a cached image?

batbat13
  • 659
  • 1
  • 9
  • 11

1 Answers1

15

Just a little modification from this existing solution https://stackoverflow.com/a/57640169/5882307 worked.

Code:

  Future<Size> _calculateImageDimension() {
  Completer<Size> completer = Completer();
  Image image = new Image(image: CachedNetworkImageProvider("https://i.stack.imgur.com/lkd0a.png")); // I modified this line
  image.image.resolve(ImageConfiguration()).addListener(
    ImageStreamListener(
      (ImageInfo image, bool synchronousCall) {
        var myImage = image.image;
        Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
        completer.complete(size);
      },
    ),
  );
  return completer.future;
}

Use as:

_calculateImageDimension().then((size) => print("size = ${size}")); // 487.0,696.0

All due credits to the original author: https://stackoverflow.com/a/57640169/5882307

OMi Shah
  • 5,768
  • 3
  • 25
  • 34