Created
August 28, 2020 05:26
-
-
Save xantiagoma/4fb419cc74bc00fef9d296766a08b33f to your computer and use it in GitHub Desktop.
SVG asset to BitmapDescriptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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