select<T> method

Future<T> select<T>(
  1. String message, {
  2. required List<T> options,
  3. T? defaultValue,
  4. String display(
    1. T item
    )?,
  5. int visibleCount = 5,
  6. bool filterable = false,
})

Lets the user pick a single value from options.

Implementation

Future<T> select<T>(
  String message, {
  required List<T> options,
  T? defaultValue,
  String Function(T item)? display,
  int visibleCount = 5,
  bool filterable = false,
}) {
  if (options.isEmpty) {
    throw ArgumentError.value(options, 'options', 'must not be empty');
  }

  final state = tui_select.SelectState<T>();
  bool submitted = false;
  T? pending;

  return runOneShot<T>(
    (ctx, submit) {
      if (submitted) {
        _drawCompactAnswer(
            ctx, message, display?.call(pending as T) ?? pending.toString());
        submit(pending as T);
        return;
      }

      final widget = tui_select.Select<T>(
        id: Key.symbol(#__inline_select),
        items: options,
        state: state,
        defaultValue: defaultValue,
        mode: tui_select.SelectionMode.single,
        visibleCount: visibleCount,
        filterable: filterable,
        builder: (item, itemState) {
          final label = display?.call(item) ?? item.toString();
          final prefix = itemState.isSelected ? '●' : '○';
          return Text(
            ' $prefix $label',
            style: itemState.isActive ? const Style(bold: true) : null,
          );
        },
        onSubmit: (list) {
          if (list.isEmpty) return;
          pending = list.first;
          submitted = true;
        },
      );
      _drawWithHeader(ctx, message, widget,
          bodyHeight:
              _selectBodyHeight(options.length, visibleCount, filterable));
    },
    theme: _theme,
    terminal: _terminal(),
    allowNonInteractive: _allowNonInteractive,
  );
}