showSingleSelectDialog function

Future<int?> showSingleSelectDialog(
  1. List<Object> options,
  2. Stream<List<int>> inputStream, {
  3. @Deprecated('Use sizing instead.') int? maxVisibleItems,
  4. SelectComponentSizing sizing = const SelectComponentSizing.fit(),
})

Shows a scrollable terminal selection dialog and returns the selected index.

Each element in options must be either a String or a SelectOption. Only standard ASCII characters and ANSI SGR styling sequences (\x1b[...m) should be used in the options (plus newlines in descriptions), as other characters may break the width calculations.

Temporarily disables stdin line and echo modes, and restores them before returning. Also intercepts ProcessSignal.sigint to cancel the dialog.

The cursor is hidden for the duration of the dialog and then re-shown. If you want the cursor in a certain state after this dialog you will have to restore it.

The inputStream is a list of input events (typically originating from stdin or Win32AnsiStdin). See the example/select_dialog.dart for recommended patterns.

The sizing parameter controls the maximum total height of the dialog and the maximum number of lines displayed for a hovered option's description. Defaults to SelectComponentSizing.fit.

The deprecated maxVisibleItems parameter is not respected strictly as an item count; if provided, it computes the maximum total height by adding the maximum description height (maxVisibleItems + maxDescriptionHeight).

Returns null if the user aborts the dialog (e.g. by pressing Ctrl+C or escape), there is no terminal attached to stdout, or the terminal is too small to display the dialog.

Implementation

Future<int?> showSingleSelectDialog(
  List<Object /* String|SelectOption */> options,
  Stream<List<int>> inputStream, {
  @Deprecated('Use sizing instead.') int? maxVisibleItems,
  SelectComponentSizing sizing = const SelectComponentSizing.fit(),
}) async {
  final selectedIndices = await _runDialog(
    options,
    inputStream,
    multiSelect: false,
    maxVisibleItems: maxVisibleItems,
    sizing: sizing,
    initialSelected: const {},
  );
  if (selectedIndices == null || selectedIndices.isEmpty) {
    return null;
  }
  return selectedIndices.single;
}