load method

Future<ImageItem> load(
  1. dynamic imageFile
)

Implementation

Future<ImageItem> load(dynamic imageFile) async {
  loader = Completer();

  if (imageFile is ImageItem) {
    height = imageFile.height;
    width = imageFile.width;

    bytes = imageFile.bytes;
    loader.complete(true);
  } else {
    bytes = imageFile;
    var decodedImage = await decodeImageFromList(bytes);

    double maxWidth = viewportSize.width;
    double maxHeight = 400;

    double aspectRatio = decodedImage.width / decodedImage.height;

    if (decodedImage.width > maxWidth || decodedImage.height > maxHeight) {
      if (decodedImage.height > maxHeight) {
        maxHeight = decodedImage.height.toDouble() - maxWidth;
      }
      if (aspectRatio > 1) {
        width = maxWidth.toInt();
        height = width ~/ aspectRatio;
      } else {
        height = maxHeight.toInt();
        width = (height * aspectRatio).toInt();
      }
    } else {
      width = decodedImage.width;
      height = decodedImage.height;
    }

    loader.complete(decodedImage);
  }

  return this;
}