choice static method
SimplePrompt<String>
choice({
- required String title,
- required List<
String> options, - int initialIndex = 0,
- PromptTheme theme = PromptTheme.dark,
Creates a single-choice prompt with inline options.
Unlike SelectableListPrompt (vertical list), this shows options inline. Best for 2-4 options that fit on one line.
final choice = SimplePrompts.choice(
title: 'Select mode',
options: ['Debug', 'Release', 'Profile'],
).run(); // Returns selected option or first on cancel
Implementation
static SimplePrompt<String> choice({
required String title,
required List<String> options,
int initialIndex = 0,
PromptTheme theme = PromptTheme.dark,
}) {
assert(options.isNotEmpty, 'Options cannot be empty');
final clampedIndex = initialIndex.clamp(0, options.length - 1);
// Use a wrapper class to hold the index since we need both index and string
return SimplePrompt<String>(
title: title,
theme: theme,
initialValue: options[clampedIndex],
buildBindings: (state) {
var currentIndex = clampedIndex;
return KeyBindings.horizontalNavigation(
onLeft: () {
currentIndex = (currentIndex - 1).clamp(0, options.length - 1);
state.value = options[currentIndex];
},
onRight: () {
currentIndex = (currentIndex + 1).clamp(0, options.length - 1);
state.value = options[currentIndex];
},
) +
KeyBindings.prompt(onCancel: state.cancel);
},
render: (ctx, state) {
final buffer = StringBuffer(' ');
for (var i = 0; i < options.length; i++) {
final opt = options[i];
final isSelected = opt == state.value;
if (isSelected) {
buffer.write(
'${ctx.theme.inverse}${ctx.theme.accent} $opt ${ctx.theme.reset}');
} else {
buffer.write('${ctx.theme.dim}$opt${ctx.theme.reset}');
}
if (i < options.length - 1) buffer.write(' ');
}
ctx.gutterLine(buffer.toString());
},
);
}