getImageWHE static method

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

Suggest use ImageUtil instead.

get image width height, load error throw exception.(unit px) Gif is not supported. 获取图片宽高,加载错误会抛出异常.(单位 px) image url network local url (full path/全路径,example:"assets/images/ali_connors.png",""assets/images/3.0x/ali_connors.png"" ); package

Implementation

static Future<Rect> getImageWHE({
  Image? image,
  String? url,
  String? localUrl,
  String? package,
  ImageConfiguration? configuration,
}) {
  if (image == null &&
      (url == null || url.isEmpty) &&
      (localUrl == null || localUrl.isEmpty)) {
    return Future.error("image is null.");
  }
  Completer<Rect> completer = Completer<Rect>();
  Image? img = image;
  if (img == null) {
    img = (url != null && url.isNotEmpty)
        ? Image.network(url)
        : Image.asset(localUrl!, package: package);
  }
  img.image
      .resolve(configuration ?? ImageConfiguration())
      .addListener(ImageStreamListener(
        (ImageInfo info, bool synchronousCall) {
          if (!completer.isCompleted) {
            completer.complete(Rect.fromLTWH(0, 0,
                info.image.width.toDouble(), info.image.height.toDouble()));
          }
        },
        onError: (dynamic exception, StackTrace? stackTrace) {
          if (!completer.isCompleted) {
            completer.completeError(exception, stackTrace);
          }
        },
      ));
  return completer.future;
}