number static method

SimplePrompt<int> number({
  1. required String title,
  2. int initial = 0,
  3. int min = 0,
  4. int max = 100,
  5. int step = 1,
  6. PromptTheme theme = PromptTheme.dark,
})

Creates a simple number input prompt.

Uses arrow keys for increment/decrement. For more features, use ValuePrompt instead.

final count = SimplePrompts.number(
  title: 'Enter count',
  initial: 5,
  min: 0,
  max: 100,
).run();

Implementation

static SimplePrompt<int> number({
  required String title,
  int initial = 0,
  int min = 0,
  int max = 100,
  int step = 1,
  PromptTheme theme = PromptTheme.dark,
}) {
  int clampValue(int value) => value.clamp(min, max).toInt();
  final interactiveStart = clampValue(initial);
  return SimplePrompt<int>(
    title: title,
    theme: theme,
    initialValue: initial,
    buildBindings: (state) {
      state.value = interactiveStart;
      return KeyBindings.horizontalNavigation(
            onLeft: () => state.value = clampValue(state.value - step),
            onRight: () => state.value = clampValue(state.value + step),
          ) +
          KeyBindings.prompt(onCancel: state.cancel);
    },
    render: (ctx, state) {
      ctx.gutterLine('${ctx.theme.accent}${state.value}${ctx.theme.reset}'
          '${ctx.theme.dim} (range: $min–$max)${ctx.theme.reset}');
    },
  );
}