createRoundedMarkerIcon function
Future<BitmapDescriptor>
createRoundedMarkerIcon(
- String title, {
- required TextStyle textStyle,
- Color backgroundColor = Colors.blueAccent,
})
Implementation
Future<BitmapDescriptor> createRoundedMarkerIcon(String title, {required TextStyle textStyle, Color backgroundColor = Colors.blueAccent}) async {
TextSpan span = TextSpan(
style: textStyle,
text: title,
);
TextPainter painter = TextPainter(
text: span,
textAlign: TextAlign.center,
textDirection: ui.TextDirection.ltr,
);
painter.text = TextSpan(
text: title.toString(),
style: textStyle,
);
painter.layout();
int textWidth = painter.width.toInt();
int textHeight = painter.height.toInt();
ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
Canvas canvas = Canvas(pictureRecorder);
RRect roundedRect = RRect.fromLTRBAndCorners(
0,
0,
textWidth + 40,
textHeight + 20,
bottomLeft: const Radius.circular(10),
bottomRight: const Radius.circular(10),
topLeft: const Radius.circular(10),
topRight: const Radius.circular(10),
);
Paint rectPaint = Paint()..color = backgroundColor;
canvas.drawRRect(roundedRect, rectPaint);
Path arrowPath = Path();
arrowPath.moveTo((textWidth + 40) / 2 - 15, textHeight + 20);
arrowPath.lineTo((textWidth + 40) / 2, textHeight + 40);
arrowPath.lineTo((textWidth + 40) / 2 + 15, textHeight + 20);
arrowPath.close();
Paint arrowPaint = Paint()..color = backgroundColor;
canvas.drawPath(arrowPath, arrowPaint);
painter.paint(canvas, const Offset(20.0, 10.0));
ui.Picture p = pictureRecorder.endRecording();
ByteData? pngBytes = await (await p.toImage((textWidth + 40), (textHeight + 50))).toByteData(format: ui.ImageByteFormat.png);
Uint8List data = Uint8List.view(pngBytes!.buffer);
return BitmapDescriptor.fromBytes(data);
}