canvasMarker function

Future<BitmapDescriptor> canvasMarker(
  1. SetUpMarker setUpMarker,
  2. String? text
)

Implementation

Future<BitmapDescriptor> canvasMarker(
  SetUpMarker setUpMarker,
  String? text,
) async {
  PictureRecorder recorder = PictureRecorder();
  Canvas canvas = Canvas(recorder);
  RenderParagraph renderParagraph = RenderParagraph(
    TextSpan(
      text: text,
      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,
  );

  TextPainter tp = TextPainter(
    text: span,
    textAlign: TextAlign.center,
    textDirection: TextDirection.ltr,
  );
  // độ dài ngắn nhất cần có để render text
  double widthText = renderParagraph.getMinIntrinsicWidth(10000).ceilToDouble();
  // độ cao ngắn nhất cần có để render text
  double heightText =
      renderParagraph.getMinIntrinsicHeight(widthText).ceilToDouble();

  tp.layout();
  // Thiết lập màu sắc, kiểu tô để vẽ
  var paintRect = Paint()
    ..color = setUpMarker.selected
        ? const Color(0xffe75c2d)
        : (setUpMarker.backGroundColor ?? Colors.white)
    ..strokeWidth = 10
    ..style = PaintingStyle.fill;

  // cách lề trên
  double paddingTop = setUpMarker.padding?.top ?? 0;
  // cách lề dưới
  double paddingBottom = setUpMarker.padding?.bottom ?? 0;
  // Cách lề trái
  double paddingLeft = setUpMarker.padding?.left ?? 0;
  // Cách lề phải
  double paddingRight = setUpMarker.padding?.right ?? 0;
  // bo tròn
  double circular = 15;

  Path path = Path();

  // Vẽ hình chữ nhật
  // Chiều cao hình chữ nhật
  double rectHeight = heightText + paddingTop + paddingBottom;
  // Chiều dài hình chữ nhật
  double rectWidth = widthText + paddingLeft + paddingRight;
  // Toạ độ cạnh trái của hình
  double leftRect = 0;
  // Toạ độ cạnh trên của hình
  double topRect = 0;
  // ignore: avoid_print
  print(topRect);
  Rect rect = Rect.fromLTWH(leftRect, topRect, rectWidth, rectHeight);
  path.addRRect(RRect.fromRectAndRadius(rect, Radius.circular(circular)));

  // Vẽ hình tam giác nhỏ ngược bên dưới
  // chiều cao
  double triaHeight = 20;
  // chiều dài
  double triaWidth = 20;
  double r = 1;
  path.moveTo(rect.bottomCenter.dx + triaWidth / 2, rect.bottomCenter.dy);
  path.relativeLineTo(-triaWidth / 2 * r, triaHeight * r);
  path.relativeQuadraticBezierTo(
      -triaWidth / 2 * (1 - r), triaHeight * (1 - r), -triaWidth * (1 - r), 0);
  path.relativeLineTo(-triaWidth / 2 * r, -triaHeight * r);

  canvas.drawPath(path, paintRect);

  // Vẽ chữ lên nền
  tp.paint(
    canvas,
    Offset(
      leftRect + paddingLeft,
      topRect + paddingTop,
    ),
  );

  // Tổng chiều dài ảnh
  int totalWidth = (widthText + paddingLeft + paddingRight).ceil();
  // Tổng chiều cao ảnh
  int totalHeight = (rectHeight + triaHeight).ceil();
  //paint cavans
  Picture p = recorder.endRecording();
  ByteData? pngBytes =
      await (await p.toImage(totalWidth, totalHeight)).toByteData(
    format: ImageByteFormat.png,
  );
  pngBytes ??= ByteData(0);
  Uint8List data = Uint8List.view(pngBytes.buffer);

  return BitmapDescriptor.fromBytes(data);
}