render method

  1. @override
void render(
  1. Buffer buffer,
  2. Rect area
)
override

Renders the underlying widget to the provided buffer within the area.

Implementation

@override
void render(Buffer buffer, Rect area) {
  final db = widget as DecoratedBox;

  // Fill background if defined
  var bgStyle = db.decoration.backgroundStyle;
  if (bgStyle == null && db.decoration.backgroundColor != null) {
    bgStyle = Style(background: db.decoration.backgroundColor);
  }
  if (bgStyle != null) {
    for (var y = 0; y < area.height; y++) {
      for (var x = 0; x < area.width; x++) {
        buffer.setCell(area.x + x, area.y + y, Cell(' ', bgStyle));
      }
    }
  }

  // Draw border if defined
  final border = db.decoration.border;
  var topOffset = 0;
  var bottomOffset = 0;
  var leftOffset = 0;
  var rightOffset = 0;

  if (border != null) {
    final style = bgStyle != null
        ? bgStyle.merge(border.style)
        : border.style;

    // Draw horizontal lines (excluding corners)
    if (border.topChar.isNotEmpty && area.height > 0) {
      topOffset = 1;
      for (var x = 1; x < area.width - 1; x++) {
        buffer.setCell(area.x + x, area.y, Cell(border.topChar, style));
      }
    }
    if (border.bottomChar.isNotEmpty && area.height > 1) {
      bottomOffset = 1;
      for (var x = 1; x < area.width - 1; x++) {
        buffer.setCell(
          area.x + x,
          area.y + area.height - 1,
          Cell(border.bottomChar, style),
        );
      }
    }

    // Draw vertical lines (excluding corners)
    if (border.leftChar.isNotEmpty && area.width > 0) {
      leftOffset = 1;
      for (var y = 1; y < area.height - 1; y++) {
        buffer.setCell(area.x, area.y + y, Cell(border.leftChar, style));
      }
    }
    if (border.rightChar.isNotEmpty && area.width > 1) {
      rightOffset = 1;
      for (var y = 1; y < area.height - 1; y++) {
        buffer.setCell(
          area.x + area.width - 1,
          area.y + y,
          Cell(border.rightChar, style),
        );
      }
    }

    // Draw corners
    if (border.topLeftChar.isNotEmpty && area.width > 0 && area.height > 0) {
      buffer.setCell(area.x, area.y, Cell(border.topLeftChar, style));
    }
    if (border.topRightChar.isNotEmpty && area.width > 1 && area.height > 0) {
      buffer.setCell(
        area.x + area.width - 1,
        area.y,
        Cell(border.topRightChar, style),
      );
    }
    if (border.bottomLeftChar.isNotEmpty &&
        area.width > 0 &&
        area.height > 1) {
      buffer.setCell(
        area.x,
        area.y + area.height - 1,
        Cell(border.bottomLeftChar, style),
      );
    }
    if (border.bottomRightChar.isNotEmpty &&
        area.width > 1 &&
        area.height > 1) {
      buffer.setCell(
        area.x + area.width - 1,
        area.y + area.height - 1,
        Cell(border.bottomRightChar, style),
      );
    }
  }

  final childX = area.x + leftOffset;
  final childY = area.y + topOffset;
  final childWidth = area.width - leftOffset - rightOffset;
  final childHeight = area.height - topOffset - bottomOffset;

  if (childWidth > 0 && childHeight > 0) {
    if (childElement != null &&
        childElement!.widget.runtimeType == db.child.runtimeType) {
      childElement!.update(db.child);
    } else {
      childElement = db.child.createElement();
      childElement!.mount(this);
    }

    final childArea = Rect(childX, childY, childWidth, childHeight);
    final childViewport = Viewport(buffer, childArea);
    childElement!.render(childViewport, Rect(0, 0, childWidth, childHeight));
  }
}