selectFromList static method

int selectFromList(
  1. String title,
  2. List<String> options, {
  3. bool multiSelect = false,
})

Displays a list of options and prompts the user to select one.

Returns the index of the selected option. The title is displayed as the list heading.

Implementation

static int selectFromList(String title, List<String> options,
    {bool multiSelect = false}) {
  print('');
  print('$bold$blue$title$reset');
  for (int i = 0; i < options.length; i++) {
    print('  $cyan${i + 1}$reset. ${options[i]}');
  }
  print('');

  while (true) {
    final input = prompt('Enter number (1-${options.length})');
    final num = int.tryParse(input ?? '');
    if (num != null && num >= 1 && num <= options.length) {
      return num - 1;
    }
    error(
        'Invalid selection. Please enter a number between 1 and ${options.length}');
  }
}