selectMultipleFromList static method
Displays a list of options and prompts the user to select multiple.
Returns a list of selected indices.
The title is displayed as the list heading.
Implementation
static List<int> selectMultipleFromList(String title, List<String> options) {
print('');
print('$bold$blue$title$reset');
print(
'$dim(Enter comma-separated numbers, e.g: 1,2,3 — or press Enter for all)$reset');
for (int i = 0; i < options.length; i++) {
print(' $cyan${i + 1}$reset. ${options[i]}');
}
print('');
while (true) {
final input = prompt('Selection');
if (input == null || input.isEmpty) {
return List.generate(options.length, (i) => i);
}
final parts = input.split(',').map((e) => e.trim()).toList();
final indices = <int>[];
bool valid = true;
for (final p in parts) {
final num = int.tryParse(p);
if (num == null || num < 1 || num > options.length) {
valid = false;
break;
}
indices.add(num - 1);
}
if (valid && indices.isNotEmpty) return indices;
error('Invalid input. Please enter valid numbers separated by commas.');
}
}