paint method

  1. @override
String paint()
override

Implementation

@override
String paint() {
  if (children.isEmpty) return '';
  final targetWidth = size.width.toInt();
  final targetHeight = size.height.toInt();
  if (targetWidth == 0 || targetHeight == 0) return '';

  final canvas = Canvas(targetWidth, targetHeight);
  final bgStyle = const UvStyle();

  var isFirstChild = true;
  for (final child in children) {
    final content = child.paint();
    final childWidth = child.size.width.toInt();
    final childHeight = child.size.height.toInt();
    final data = child.parentData as StackParentData?;
    var x = 0;
    var y = 0;

    if (data != null && data.isPositioned) {
      final left = _resolveDimension(data.left);
      final right = _resolveDimension(data.right);
      final top = _resolveDimension(data.top);
      final bottom = _resolveDimension(data.bottom);
      if (left != null) {
        x = left;
      } else if (right != null) {
        x = targetWidth - childWidth - right;
      } else {
        x = ((alignment.x + 1) / 2 * (targetWidth - childWidth)).round();
      }

      if (top != null) {
        y = top;
      } else if (bottom != null) {
        y = targetHeight - childHeight - bottom;
      } else {
        y = ((alignment.y + 1) / 2 * (targetHeight - childHeight)).round();
      }
    } else {
      x = ((alignment.x + 1) / 2 * (targetWidth - childWidth)).round();
      y = ((alignment.y + 1) / 2 * (targetHeight - childHeight)).round();
    }

    if (isFirstChild &&
        x == 0 &&
        y == 0 &&
        childWidth == targetWidth &&
        childHeight == targetHeight) {
      // First child fills the entire canvas — draw StyledString directly
      // onto the main canvas, skipping the temp canvas + cell-by-cell copy.
      StyledString(content).draw(canvas, canvas.bounds());
    } else {
      _drawStyledContent(
        canvas,
        content,
        x,
        y,
        bgStyle,
        transparent: !isFirstChild,
      );
    }
    isFirstChild = false;
  }

  var result = canvas.render();
  result = _padToStackSize(result, targetWidth, targetHeight);
  return result;
}