getImageSize static method

Future<Size> getImageSize(
  1. Object image
)

获取图片宽高

Implementation

static Future<Size> getImageSize(Object image) async {
  final Completer<Size> completer = Completer();

  late ImageProvider imageProvider;
  if (image is String) {
    // 网络图片
    imageProvider = NetworkImage(image);
  } else if (image is File) {
    // 文件图片
    imageProvider = FileImage(image);
  } else if (image is AssetImage) {
    // Asset 图片
    imageProvider = image;
  } else {
    throw ArgumentError('Unsupported image type');
  }

  imageProvider.resolve(const ImageConfiguration()).addListener(
    ImageStreamListener((ImageInfo info, bool _) {
      completer.complete(
          Size(info.image.width.toDouble(), info.image.height.toDouble()));
    }),
  );

  return completer.future;
}