render method

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

Implementation

@override
void render(Rect area, Buffer buffer, RenderContext ctx) {
  if (area.isEmpty) return;
  final focused = ctx.isFocused(id);

  final label = showValue ? ' ${_format(state.value)}' : '';
  final trackWidth = (area.width - label.length).clamp(1, area.width);
  if (trackWidth <= 0) return;

  final ratio = (state.value - min) / (max - min);
  final handlePos =
      (ratio * (trackWidth - 1)).round().clamp(0, trackWidth - 1);

  final track = trackStyle ?? Style(fg: ctx.theme.colors.muted);
  final fill = fillStyle ?? Style(fg: ctx.theme.colors.primary);
  final handle = handleStyle ??
      Style(
        fg: focused ? ctx.theme.colors.primary : ctx.theme.colors.foreground,
        bold: focused,
      );

  for (var i = 0; i < trackWidth; i++) {
    if (i == handlePos) {
      buffer.setChar(area.x + i, area.y, handleChar, style: handle);
    } else if (i < handlePos) {
      buffer.setChar(area.x + i, area.y, fillChar, style: fill);
    } else {
      buffer.setChar(area.x + i, area.y, trackChar, style: track);
    }
  }

  if (label.isNotEmpty) {
    buffer.writeText(area.x + trackWidth, area.y, label,
        style: ctx.theme.text.body, maxWidth: label.length);
  }
}