drawText method

void drawText(
  1. String text,
  2. Offset position, {
  3. TextStyle? style,
  4. Alignment alignment = Alignment.centerLeft,
  5. TextAlign textAlign = TextAlign.left,
  6. int maxLines = 1,
})

Implementation

void drawText(
    String text,
    Offset position,
    {
      TextStyle? style,
      Alignment alignment = Alignment.centerLeft,
      TextAlign textAlign = TextAlign.left,
      int maxLines = 1,
    }
    ) {

  final textSpan = TextSpan(
    text: text,
    style: style,
  );

  final textPainter = TextPainter(
    text: textSpan,
    textDirection: TextDirection.ltr,
    textAlign: textAlign,
    maxLines: maxLines,
  );

  textPainter.layout();

  final double x, y;

  if (alignment.x < 0) {
    x = position.dx;
  } else if (alignment.x > 0) {
    x = position.dx - textPainter.width;
  } else {
    x = position.dx - textPainter.width * 0.5;
  }

  if (alignment.y < 0) {
    y = position.dy - textPainter.height;
  } else if (alignment.y > 0) {
    y = position.dy + textPainter.height;
  } else {
    y = position.dy - textPainter.height * 0.5;
  }

  textPainter.paint(this, Offset(x, y));
}