createCanvasImage method

  1. @override
PCanvasImage createCanvasImage(
  1. Object source, {
  2. int? width,
  3. int? height,
})
override

Creates a PCanvasImage instance compatible to this canvas and its painter.

Implementation

@override
PCanvasImage createCanvasImage(Object source, {int? width, int? height}) {
  var id = ++_imageIdCount;

  var src = source;

  if (source is String) {
    if (source.startsWith('http://') || source.startsWith('https://')) {
      source = NetworkImage(source);
    }
  }

  if (source is ImageProvider) {
    var imageConfig = const ImageConfiguration();
    var imageStream = source.resolve(imageConfig);

    var completer = Completer<ui.Image>();

    imageStream.addListener(ImageStreamListener((imageInfo, _) {
      completer.complete(imageInfo.image);
    }, onError: (e, s) {
      completer.completeError(e, s);
    }));

    return _PCanvasImageFlutterAsync('img_$id', completer.future, '$src');
  } else if (source is ui.Image) {
    return _PCanvasImageFlutterSync('img_$id', source, '[image]');
  } else if (source is Uint8List) {
    var imageFuture = decodeImageFromList(source);
    return _PCanvasImageFlutterAsync(
        'img_$id', imageFuture, '[bytes:${source.length}]');
  }

  throw ArgumentError("Can't handle image source: $source");
}