performPaint method

  1. @override
void performPaint(
  1. Buffer buffer,
  2. Offset offset
)
override

Hook for subclasses to implement actual painting.

Implementation

@override
void performPaint(Buffer buffer, Offset offset) {
  final richTextWidget = widget as RichText;
  final area = Rect(offset.dx, offset.dy, size.width, size.height);

  final limit = richTextWidget.maxLines != null
      ? min(richTextWidget.maxLines!, area.height)
      : area.height;
  for (var y = 0; y < _cachedLines.length; y++) {
    if (y >= limit) break;
    final line = _cachedLines[y];

    // Compute horizontal alignments
    final lineLen = line.length;
    var startX = 0;
    if (richTextWidget.textAlign == TextAlign.right) {
      startX = max(0, area.width - lineLen);
    } else if (richTextWidget.textAlign == TextAlign.center) {
      startX = max(0, (area.width - lineLen) ~/ 2);
    }

    for (var x = 0; x < line.length; x++) {
      if (startX + x >= area.width) break;
      buffer.setAttributes(
        area.x + startX + x,
        area.y + y,
        char: line[x].char,
        fg: line[x].style.foreground?.argb ?? 0,
        bg: line[x].style.background?.argb ?? 0,
        modifiers: line[x].style.modifiers,
      );
    }
  }
}