getImageWH static method

Future<Rect> getImageWH({
  1. Image? image,
  2. String? url,
  3. String? localUrl,
  4. String? package,
})

get image width height,load error return Rect.zero.(unit px) 获取图片宽高,加载错误情况返回 Rect.zero.(单位 px) image url network local url , package

Implementation

static Future<Rect> getImageWH(
    {Image? image, String? url, String? localUrl, String? package}) {
  if (ObjectUtils.isEmpty(image) &&
      ObjectUtils.isEmpty(url) &&
      ObjectUtils.isEmpty(localUrl)) {
    return Future.value(Rect.zero);
  }
  Completer<Rect> completer = Completer<Rect>();
  Image img = image ?? ((url != null && url.isNotEmpty)
          ? Image.network(url)
          : Image.asset(localUrl!, package: package));
  img.image
      .resolve(const ImageConfiguration())
      .addListener(ImageStreamListener(
        (ImageInfo info, bool _) {
          completer.complete(Rect.fromLTWH(0, 0, info.image.width.toDouble(),
              info.image.height.toDouble()));
        },
        onError: (Object exception, StackTrace? stackTrace) {
          completer.completeError(exception, stackTrace);
        },
      ));
  return completer.future;
}