yang-f / flutter_svg_provider

Use svg as image provider.
https://pub.dev/packages/flutter_svg_provider
Apache License 2.0
47 stars 72 forks source link

Problem with scaling since flutter_svg 2.0 #47

Closed pbouttier closed 10 months ago

pbouttier commented 1 year ago

Hi,

With flutter_svg 2.0+ we've lost the .toPicture, and its size method. This poses a problem when upscaling images, as the scale is not good (upscaled images can be blurred/pixelated). The problem has been discussed here https://github.com/dnfield/flutter_svg/issues/858

To get around this, I had to do the following:

  static Future<ImageInfo> _loadAsync(SvgImageKey key) async {
    final String rawSvg = await _getSvgString(key);
    final pictureInfo = await vg.loadPicture(
      SvgStringLoader(rawSvg),
      null,
      clipViewbox: false,
    );

    final ui.PictureRecorder recorder = ui.PictureRecorder();
    final ui.Canvas canvas = ui.Canvas(recorder);

    canvas.scale(key.pixelWidth / pictureInfo.size.width, key.pixelHeight / pictureInfo.size.height);
    canvas.drawPicture(pictureInfo.picture);
    final ui.Picture scaledPicture = recorder.endRecording();

    final image = await scaledPicture.toImage(key.pixelWidth, key.pixelHeight);

    return ImageInfo(
      image: image,
      scale: 1.0,
    );
  }

I tried to manipulate all the other methods scale, size, ... without success, only this fix solved my problem.

Is there a better solution?

fisforfaheem commented 1 year ago

please tell how to fix this: I/flutter ( 7924): unhandled element filter; Picture key: Svg loader

pbouttier commented 9 months ago

Same problem in latest version