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;

  if (source is Uint8List) {
    var image = img.decodeImage(source);
    return _PCanvasImageMemorySync('img_$id', image!, '[bytes]');
  } else if (source is String) {
    if (source.startsWith('http://') || source.startsWith('https://')) {
      var uri = Uri.parse(source);

      var imageFuture = Dio()
          .getUri(uri, options: Options(responseType: ResponseType.bytes))
          .then((response) {
        var bytes = response.data as List<int>;
        var data = bytes is Uint8List ? bytes : Uint8List.fromList(bytes);
        return img.decodeImage(data)!;
      });

      return _PCanvasImageMemoryAsync('img_$id', imageFuture, '$uri');
    }
  }

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