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 =
      showValues ? ' ${_format(state.low)}–${_format(state.high)}' : '';
  final trackWidth = (area.width - label.length).clamp(1, area.width);
  if (trackWidth <= 0) return;

  final rangeSpan = max - min;
  final lowPos = ((state.low - min) / rangeSpan * (trackWidth - 1))
      .round()
      .clamp(0, trackWidth - 1);
  final highPos = ((state.high - min) / rangeSpan * (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 defaultHandle = Style(
    fg: focused ? ctx.theme.colors.primary : ctx.theme.colors.foreground,
  );
  final defaultActive = Style(
    fg: ctx.theme.colors.primary,
    bold: true,
  );
  final h = handleStyle ?? defaultHandle;
  final ah = activeHandleStyle ?? defaultActive;

  for (var i = 0; i < trackWidth; i++) {
    String ch;
    Style s;
    if (i == lowPos) {
      ch = state.active == RangeHandle.low && focused
          ? activeHandleChar
          : handleChar;
      s = state.active == RangeHandle.low && focused ? ah : h;
    } else if (i == highPos) {
      ch = state.active == RangeHandle.high && focused
          ? activeHandleChar
          : handleChar;
      s = state.active == RangeHandle.high && focused ? ah : h;
    } else if (i > lowPos && i < highPos) {
      ch = fillChar;
      s = fill;
    } else {
      ch = trackChar;
      s = track;
    }
    buffer.setChar(area.x + i, area.y, ch, style: s);
  }

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