createLocationMarkerBitmap function

Future<BitmapDescriptor> createLocationMarkerBitmap(
  1. String title, {
  2. required TextStyle textStyle,
  3. Color backgroundColor = const Color(0XFF3644ff),
})

Implementation

Future<BitmapDescriptor> createLocationMarkerBitmap(String title, {required TextStyle textStyle, Color backgroundColor = const Color(0XFF3644ff)}) async {
  double outerRadius = 20.0;
  double innerRadius = 10.0;

  ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
  Canvas canvas = Canvas(pictureRecorder);

  Paint outerCirclePaint = Paint()..color = backgroundColor;
  canvas.drawCircle(Offset(outerRadius, outerRadius), outerRadius, outerCirclePaint);

  Paint innerCirclePaint = Paint()..color = Colors.white;
  canvas.drawCircle(Offset(outerRadius, outerRadius), innerRadius, innerCirclePaint);

  TextSpan span = TextSpan(
    style: textStyle,
    text: title,
  );
  TextPainter textPainter = TextPainter(
    text: span,
    textAlign: TextAlign.center,
    textDirection: ui.TextDirection.ltr,
  );
  textPainter.layout();
  textPainter.paint(canvas, Offset(outerRadius - textPainter.width / 2, outerRadius - textPainter.height / 2));

  ui.Picture picture = pictureRecorder.endRecording();
  ui.Image image = await picture.toImage((outerRadius * 2).toInt(), (outerRadius * 2).toInt());
  ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  Uint8List imageData = byteData!.buffer.asUint8List();

  return BitmapDescriptor.fromBytes(imageData);
}