build method
Future<BitmapDescriptor>
build(
)
Implementation
Future<BitmapDescriptor> build() async {
String svgString;
final mediaQueryData = MediaQuery.of(_context);
if (_assetName != null) {
svgString = await DefaultAssetBundle.of(_context).loadString(_assetName!);
} else if (_svgString != null) {
svgString = _svgString!;
} else {
svgString = _dummySvg;
}
if (_interpolateParams != null) {
svgString = svgString.interpolate(_interpolateParams!);
}
//DrawableRoot drawableRoot = await svg.fromSvgString(svgString, "");
final pictureInfo = await vg.loadPicture(SvgStringLoader(svgString), null);
// double width = drawableRoot.viewport.width;
// double height = drawableRoot.viewport.height;
double width = pictureInfo.size.width;
double height = pictureInfo.size.height;
if (_originalSizeAsLp) {
double ratio = mediaQueryData.devicePixelRatio;
width = width * ratio;
height = height * ratio;
}
if (_originalSizeAsSp) {
double ratio = mediaQueryData.devicePixelRatio * mediaQueryData.textScaleFactor;
width = width * ratio;
height = height * ratio;
} else if (_targetLpWidth > 0) {
double pxWidth = _targetLpWidth * mediaQueryData.devicePixelRatio;
if (pxWidth.round() != width.round()) {
height = height * pxWidth / width;
width = pxWidth.toDouble();
}
} else if (_targetSpWidth > 0) {
double pxWidth = _targetSpWidth * mediaQueryData.devicePixelRatio * mediaQueryData.textScaleFactor;
if (pxWidth.round() != width.round()) {
height = height * pxWidth / width;
width = pxWidth.toDouble();
}
} else if (_targetPxWidth > 0) {
if (_targetPxWidth != width.round()) {
height = height * _targetPxWidth / width;
width = _targetPxWidth.toDouble();
}
}
BitmapDescriptor? bitmapDescriptor;
bool hit = false;
if (_useCache) {
bitmapDescriptor = bitmapDescriptorCache.get(svgString, width.round(), height.round());
if (bitmapDescriptor == null) {
hit = false;
//ui.Picture picture = drawableRoot.toPicture(size: Size(width, height));
ui.Image image = await pictureInfo.picture.toImage(width.round(), height.round());
ByteData? bytes = await image.toByteData(format: ui.ImageByteFormat.png);
bitmapDescriptor = BitmapDescriptor.fromBytes(bytes!.buffer.asUint8List());
bitmapDescriptorCache.set(svgString, width.round(), height.round(), bitmapDescriptor);
} else {
hit = true;
}
} else {
// ui.Picture picture = drawableRoot.toPicture(size: Size(width, height));
ui.Image image = await pictureInfo.picture.toImage(width.round(), height.round());
ByteData? bytes = await image.toByteData(format: ui.ImageByteFormat.png);
bitmapDescriptor = BitmapDescriptor.fromBytes(bytes!.buffer.asUint8List());
}
if (_debugLog) {
debugUtil.log("BitmapDescriptorFromSvgAssetBuilder");
debugUtil.log("devicePixelRatio ${mediaQueryData.devicePixelRatio}");
debugUtil.log("assetName $_assetName");
debugUtil.log("svgString $svgString");
debugUtil.log("interpolateParams $_interpolateParams");
debugUtil.log("originalSizeAsLp $_originalSizeAsLp");
debugUtil.log("targetLpWidth $_targetLpWidth");
debugUtil.log("targetPxWidth $_targetPxWidth");
debugUtil.log("output width ${width.round()}");
debugUtil.log("output height ${height.round()}");
debugUtil.log("_useCache $_useCache hit $hit");
}
pictureInfo.picture.dispose();
return bitmapDescriptor;
}