canvasMarkerDuAn function

Future<BitmapDescriptor> canvasMarkerDuAn(
  1. SetUpMarker setUpMarker,
  2. String text,
  3. String soCan
)

Implementation

Future<BitmapDescriptor> canvasMarkerDuAn(
    SetUpMarker setUpMarker, String text, String soCan) async {
  PictureRecorder recorder = PictureRecorder();
  Canvas canvas = Canvas(recorder);
  RenderParagraph renderParagraph = RenderParagraph(
    TextSpan(
      text: '$text\n$soCan',
      style: TextStyle(
          fontSize: setUpMarker.fontSize, fontWeight: setUpMarker.fontWeight),
    ),
    textDirection: ui.TextDirection.ltr,
    maxLines: 1,
  );

  final paintB = Paint()
    ..color = Colors.transparent
    ..strokeWidth = 10
    ..style = PaintingStyle.fill;

  TextSpan span = TextSpan(
      style: TextStyle(
          color: setUpMarker.textColor,
          fontSize: setUpMarker.fontSize,
          fontWeight: setUpMarker.fontWeight,
          background: paintB),
      text: '$text\n$soCan');

  TextPainter tp = TextPainter(
    text: span,
    textAlign: TextAlign.center,
    textDirection: TextDirection.ltr,
  );

  double widthText = renderParagraph.getMinIntrinsicWidth(10000).ceilToDouble();

  double heightText =
      renderParagraph.getMinIntrinsicHeight(widthText).ceilToDouble();
  tp.layout();
  var paintRect = Paint()
    ..color = setUpMarker.selected
        ? const Color(0xffe75c2d)
        : (setUpMarker.backGroundColor ?? Colors.white)
    ..strokeWidth = 10
    ..style = PaintingStyle.fill;
  final markerWidth = -widthText / 2;
  const markerHeight = 200.0;
  const paddingTop = 5.0;
  const rectTextHeight = 80.0;
  const circular = 15.0;
  const paddingRect = circular * 1.75;
  final leftRect = widthText + markerWidth - paddingRect;
  final totalWidth = (widthText.toInt()) * 2 +
      Methods.toInt((markerWidth + paddingRect).toString());

  final totalWidthRect = totalWidth - leftRect - circular;

  Rect rect =
      Rect.fromLTWH(leftRect, paddingTop, totalWidthRect, rectTextHeight);
  double x = 20, y = 20, r = 1;

  Path path = Path()
    ..addRRect(RRect.fromRectAndRadius(rect, const Radius.circular(circular)))
    ..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);

  canvas.drawPath(path, paintRect);

  tp.paint(
      canvas,
      Offset((widthText + markerWidth - 5),
          (rectTextHeight - heightText - 15) / 2.0));

//paint cavans
  Picture p = recorder.endRecording();
  ByteData? pngBytes = await (await p.toImage(
          totalWidth, Methods.toInt(markerHeight.toString())))
      .toByteData(
    format: ImageByteFormat.png,
  );
  pngBytes ??= ByteData(0);
  Uint8List data = Uint8List.view(pngBytes.buffer);
  return BitmapDescriptor.fromBytes(data);
}