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 prefix = state.submitted
      ? successPrefix
      : (state.error != null ? errorPrefix : askPrefix);
  final prefixStyle = state.submitted
      ? (successPrefixStyle ??
          Style(fg: ctx.theme.colors.success, bold: true))
      : (state.error != null
          ? (errorPrefixStyle ??
              Style(fg: ctx.theme.colors.error, bold: true))
          : (askPrefixStyle ??
              Style(fg: ctx.theme.colors.primary, bold: true)));

  final msgStyle = messageStyle ?? ctx.theme.text.body;
  // Field style matches DatePicker / TimePicker: when focused (and not
  // yet submitted), the numeric value sits in a coloured cell so the
  // user sees clearly where typing lands.
  final activeFieldStyle = inputStyle ??
      Style(
        fg: ctx.theme.colors.background,
        bg: ctx.theme.colors.primary,
        bold: true,
      );
  final restingValueStyle =
      inputStyle ?? ctx.theme.text.body.copyWith(bold: true);

  var x = area.x;
  buffer.writeText(x, area.y, prefix,
      style: prefixStyle, maxWidth: area.width);
  x += prefix.length + 1;

  if (x < area.right) {
    buffer.writeText(x, area.y, message,
        style: msgStyle, maxWidth: area.right - x);
    x += message.length + 1;
  }

  if (x < area.right) {
    final showPlaceholder =
        state.text.isEmpty && placeholder != null && !focused;
    if (showPlaceholder) {
      buffer.writeText(x, area.y, placeholder!,
          style: Style(fg: ctx.theme.colors.muted, italic: true),
          maxWidth: area.right - x);
    } else {
      // Empty + focused → still show one-cell cursor block so the user
      // sees where typing lands; otherwise size the bg to the value.
      final value = state.text.isEmpty && focused && !state.submitted
          ? ' '
          : state.text;
      final style = (focused && !state.submitted)
          ? activeFieldStyle
          : restingValueStyle;
      buffer.writeText(x, area.y, value,
          style: style, maxWidth: area.right - x);
    }
  }

  if (!state.submitted && state.error != null && area.height >= 2) {
    final errStyle = errorStyle ?? Style(fg: ctx.theme.colors.error);
    buffer.writeText(area.x + prefix.length + 1, area.y + 1, state.error!,
        style: errStyle, maxWidth: area.width - prefix.length - 1);
  }
}