render method

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

Renders the widget onto the provided buffer within the specified area.

Implementation

@override
void render(Buffer buffer, Rect area) {
  _lastArea = area;
  if (area.width <= 0 || area.height <= 0) return;

  final trackHeight = direction == LayoutDirection.vertical
      ? area.height
      : area.width;

  final total = _totalExtent;
  final view = _viewportExtent;
  if (total <= 0) return;

  // 1. Calculate thumb size
  final double ratio = (view / total).clamp(0.0, 1.0);
  final int thumbHeight = (ratio * trackHeight).round().clamp(1, trackHeight);

  // 2. Calculate thumb position
  final int maxScrollOffset = total - view;
  int thumbPos = 0;
  if (maxScrollOffset > 0) {
    final double scrollRatio = (_scrollOffset / maxScrollOffset).clamp(
      0.0,
      1.0,
    );
    thumbPos = (scrollRatio * (trackHeight - thumbHeight)).round().clamp(
      0,
      trackHeight - thumbHeight,
    );
  }

  if (direction == LayoutDirection.vertical) {
    for (var y = 0; y < trackHeight; y++) {
      final isThumb = y >= thumbPos && y < thumbPos + thumbHeight;
      final cell = buffer.getCell(0, y);
      if (cell != null) {
        cell.char = isThumb ? thumbChar : trackChar;
        cell.style = isThumb ? thumbStyle : trackStyle;
      }
    }
  } else {
    for (var x = 0; x < trackHeight; x++) {
      final isThumb = x >= thumbPos && x < thumbPos + thumbHeight;
      final cell = buffer.getCell(x, 0);
      if (cell != null) {
        cell.char = isThumb ? thumbChar : trackChar;
        cell.style = isThumb ? thumbStyle : trackStyle;
      }
    }
  }
}