drawPin method

  1. @override
void drawPin(
  1. Canvas canvas,
  2. Size size,
  3. String text,
  4. int pinLength,
  5. Cursor? cursor,
)
override

Implementation

@override
void drawPin(
    Canvas canvas,
    Size size,
    String text,
    int pinLength,
    Cursor? cursor,
    ) {

  double mainHeight = 50;

  Paint borderPaint = Paint()
    ..strokeWidth = strokeWidth
    ..style = PaintingStyle.stroke
    ..isAntiAlias = true;

  double gapTotalLength =
      gapSpaces?.reduce((a, b) => a + b) ?? (pinLength - 1) * gapSpace;

  List<double> actualGapSpaces =
      gapSpaces ?? List.filled(pinLength - 1, gapSpace);

  double singleWidth = (size.width - strokeWidth * 2 * pinLength - gapTotalLength) / pinLength;

  var startX = strokeWidth / 2;
  var startY = mainHeight - strokeWidth / 2;

  for (int i = 0; i < pinLength; i++) {
    if (errorText != null && errorText!.isNotEmpty) {
      borderPaint.color = errorTextStyle?.color ?? strokeColorBuilder.indexProperty(i);
    } else {
      borderPaint.color = strokeColorBuilder.indexProperty(i);
    }

    RRect rRect = RRect.fromRectAndRadius(
        Rect.fromLTRB(
          startX,
          strokeWidth / 2,
          startX + singleWidth + strokeWidth,
          startY,
        ),
        radius
    );

    canvas.drawRRect(rRect, borderPaint);

    startX += singleWidth + strokeWidth * 2 + (i == pinLength - 1 ? 0 : actualGapSpaces[i]);
  }

  var index = 0;
  startY = 0.0;

  TextPainter textPainter;

  for (var rune in text.runes) {
    String code = String.fromCharCode(rune);

    textPainter = TextPainter(
      text: TextSpan(
        style: textStyle,
        text: code,
      ),
      textAlign: TextAlign.center,
      textDirection: TextDirection.ltr,
    );

    textPainter.layout();

    if (startY == 0.0) {
      startY = mainHeight / 2 - textPainter.height / 2;
    }
    startX = singleWidth * index +
        singleWidth / 2 -
        textPainter.width / 2 +
        actualGapSpaces.take(index).sumList() +
        strokeWidth * index * 2 +
        strokeWidth;
    textPainter.paint(canvas, Offset(startX, startY));
    index++;
  }
}