paint method

  1. @override
String paint()
override

Implementation

@override
String paint() {
  if (children.isEmpty) return '';

  // Use cached content lines if available; otherwise paint the child and
  // cache the result.
  final lines = cachedContentLines;
  if (lines.isEmpty) return '';

  final viewportHeight = size.height.round();
  final scrollOffset = _controller.offset.clamp(0, _controller.maxOffset);

  // Skip `scrollOffset` lines, take `viewportHeight` lines.
  if (scrollOffset >= lines.length) return '';
  final endLine = math.min(lines.length, scrollOffset + viewportHeight);
  var visible = lines.sublist(scrollOffset, endLine);

  // Apply selection highlighting if controller has an active selection.
  if (_controller is WidgetScrollController) {
    final ctrl = _controller as WidgetScrollController;
    if (ctrl.hasSelection) {
      visible = _applySelectionHighlighting(visible, scrollOffset, ctrl);
    }
  }

  // If fewer lines than viewport, pad with empty lines to fill height.
  while (visible.length < viewportHeight) {
    visible.add('');
  }

  return visible.join('\n');
}