据我所知,您只能使用标记图标来做到这一点,方法是使用画家将您的文本渲染为图像并将此图像显示为标记的图标。
有关示例,请参阅 https://stackoverflow.com/a/63201170/1151983 或 https://stackoverflow.com/a/58173697/1151983。
更新:以下示例展示了如何在同一画布上使用多个画家。第一位画家创建了一个价格标签标记。第二个将品牌名称写在画布上。在您的示例中,这可能是标签。
Future<BitmapDescriptor> _getSinglePriceTag({
required String price,
required String brandName,
}) async {
String resolutionPathModifier;
resolutionPathModifier = _getResolutionPathModifier(pixelRatio);
final singleTagImage =
await load('assets/tags/${resolutionPathModifier}pricetag_head.png');
final superScriptPrice = getSuperScriptPrice(price);
final width = singleTagImage.width.toDouble();
final height = singleTagImage.height.toDouble();
final widthAsInt = width.floor();
final heightAsInt = height.floor();
final pictureRecorder = ui.PictureRecorder();
final canvas = Canvas(pictureRecorder);
final priceTagPainter = PriceTagPainter(
singleTagImage,
price: superScriptPrice,
pixelRatio: pixelRatio,
);
final brandPainter = BrandPainter(
singleTagImage,
pixelRatio: pixelRatio,
brandName: brandName,
);
priceTagPainter.paint(canvas, Size(width, height));
brandPainter.paint(canvas, Size(width, height));
final recordedPicture = pictureRecorder.endRecording();
final img = await recordedPicture.toImage(widthAsInt, heightAsInt);
final data = await img.toByteData(format: ui.ImageByteFormat.png);
return BitmapDescriptor.fromBytes(data!.buffer.asUint8List());
}
画家可能是这样的。
class PriceTagPainter extends CustomPainter {
PriceTagPainter(
this.priceTagImage, {
required this.price,
required this.pixelRatio,
});
static const textStyle = TextStyle(
color: Colors.black,
fontFamily: 'OpenSans',
);
final String price;
final double pixelRatio;
final ui.Image priceTagImage;
@override
void paint(Canvas canvas, Size size) {
canvas.drawImage(priceTagImage, Offset.zero, Paint());
final textSpan = TextSpan(
text: price,
style: textStyle,
);
final textPainter = TextPainter(
text: textSpan,
textDirection: TextDirection.ltr,
textScaleFactor: pixelRatio,
)..layout(
maxWidth: size.width,
);
final dx = (size.width - textPainter.width + 5) * 0.5;
final dy = (size.height - textPainter.height) * 0.6;
final offset = Offset(dx, dy);
textPainter.paint(canvas, offset);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}
它创造了这样的东西。