createUIImageFromSvgAsset static method

Future<Image> createUIImageFromSvgAsset(
  1. String svgAsset, {
  2. int height = 50,
  3. int width = 50,
})

Creates an Image object from an SVG asset.

This method loads an SVG from the provided svgAsset path and converts it into a raster Image of the specified height and width.

  • svgAsset: The asset path of the SVG to be loaded.
  • height: The target height of the image. Defaults to 50.
  • width: The target width of the image. Defaults to 50.

Returns a Future that completes with the rasterized Image.

Implementation

static Future<Image> createUIImageFromSvgAsset(
  String svgAsset, {
  int height = 50,
  int width = 50,
}) async {
  final PictureInfo pictureInfo =
      await vg.loadPicture(SvgAssetLoader(svgAsset), null);

  var pictureWidth = pictureInfo.size.width.toInt();
  var pictureHeight = pictureInfo.size.height.toInt();
  final Image image = await pictureInfo.picture.toImage(pictureWidth, pictureHeight);

  pictureInfo.picture.dispose();

  final resizedImage = await _resizeSvg(image, width, height, pictureWidth, pictureHeight);
  return resizedImage ?? image;
}