Skip to content

Instantly share code, notes, and snippets.

@xantiagoma
Created August 28, 2020 05:26
Show Gist options
  • Save xantiagoma/4fb419cc74bc00fef9d296766a08b33f to your computer and use it in GitHub Desktop.
Save xantiagoma/4fb419cc74bc00fef9d296766a08b33f to your computer and use it in GitHub Desktop.
SVG asset to BitmapDescriptor
Future<BitmapDescriptor> getBitmapDescriptorFromSVGAsset(
BuildContext context,
String svgAssetLink, {
Size size = const Size(30, 30),
}) async {
String svgString = await DefaultAssetBundle.of(context).loadString(
svgAssetLink,
);
final drawableRoot = await svg.fromSvgString(
svgString,
'debug: $svgAssetLink',
);
final ratio = ui.window.devicePixelRatio.ceil();
final width = size.width.ceil() * ratio;
final height = size.height.ceil() * ratio;
final picture = drawableRoot.toPicture(
size: Size(
width.toDouble(),
height.toDouble(),
),
);
final image = await picture.toImage(width, height);
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
final uInt8List = byteData.buffer.asUint8List();
return BitmapDescriptor.fromBytes(uInt8List);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment