getMarkerBitmap function

Future<BitmapDescriptor> getMarkerBitmap({
  1. double? size,
  2. required String text,
  3. required Color color,
  4. double strokeWidth = 4,
  5. Color strokeColor = Colors.white,
  6. Color textColor = Colors.white,
  7. TextDirection textDirection = TextDirection.rtl,
  8. String? familyFont,
})

Implementation

Future<BitmapDescriptor> getMarkerBitmap({
  double? size,
  required String text,
  required Color color,
  double strokeWidth = 4,
  Color strokeColor = Colors.white,
  Color textColor = Colors.white,
  TextDirection textDirection = TextDirection.rtl,
  String? familyFont,
}) async {
  size ??= text.getWidth();
  double width = size * 3;
  double height = 75;

  double radius = size / 8;
  double arrowWidth = height / 3;
  double arrowHeight = height / 5;
  double arrowRadius = 0.0;

  double shadowOffset = 10.0;

  final PictureRecorder pictureRecorder = PictureRecorder();
  final Canvas canvas = Canvas(pictureRecorder);
  final Paint paint = Paint()..color = color;

  /// if right is less than left or bottom is less than top then the rectangle is not drawn
  var rect = Rect.fromLTRB(width - 4, height - 4, 4, 4);

  rect = Rect.fromPoints(rect.topLeft, rect.bottomRight);
  double x = arrowWidth, y = arrowHeight, r = 1 - arrowRadius;
  var path = Path()
    ..moveTo(rect.bottomCenter.dx + x / 2, rect.bottomCenter.dy)
    ..relativeLineTo(-x / 2 * r, y * r)
    ..relativeQuadraticBezierTo(-x / 2 * (1 - r), y * (1 - r), -x * (1 - r), 0)
    ..relativeLineTo(-x / 2 * r, -y * r)
    ..addRRect(RRect.fromRectAndRadius(rect, Radius.circular(radius)))
    ..close();

  /// border
  Paint borderPaint = Paint()
    ..color = strokeColor
    ..style = PaintingStyle.stroke
    ..strokeJoin = StrokeJoin.round
    ..strokeWidth = strokeWidth;

  canvas.drawPath(path, borderPaint);

  /// shadow
  // canvas.drawShadow(
  //     path.shift(Offset(0, shadowOffset)), Colors.black, 2, true);

  /// draw speech dialog
  canvas.drawPath(path, paint);

  /// text
  TextPainter painter = TextPainter(textDirection: textDirection);
  painter.text = TextSpan(
    text: text,
    style: TextStyle(
      fontSize: 35,
      color: textColor,
      fontWeight: FontWeight.bold,
      fontFamily: familyFont,
    ),
  );
  painter.layout();
  painter.paint(
    canvas,
    Offset((width / 2 - painter.width / 2), height / 2 - painter.height / 2),
  );

  final img = await pictureRecorder
      .endRecording()
      .toImage(width.toInt(), (height + arrowHeight + shadowOffset).toInt());
  final data = await img.toByteData(format: ImageByteFormat.png) as ByteData;

  return BitmapDescriptor.fromBytes(data.buffer.asUint8List());
}