run method

num run({
  1. required void render(
    1. FrameContext ctx,
    2. num value,
    3. double ratio
    ),
  2. KeyBindings? extraBindings,
  3. bool useNumberKeys = false,
  4. int numberKeyMax = 9,
})

Runs the value prompt with custom rendering.

Implementation

num run({
  required void Function(FrameContext ctx, num value, double ratio) render,
  KeyBindings? extraBindings,
  bool useNumberKeys = false,
  int numberKeyMax = 9,
}) {
  _initState();

  _bindings = KeyBindings.horizontalNavigation(
    onLeft: () => _value = normalizeSteppedValue(
      _value - step,
      min: min,
      max: max,
      step: step,
    ),
    onRight: () => _value = normalizeSteppedValue(
      _value + step,
      min: min,
      max: max,
      step: step,
    ),
  );

  if (useNumberKeys) {
    _bindings = _bindings +
        KeyBindings.numbers(
          onNumber: (n) {
            if (n >= 1 && n <= numberKeyMax) {
              final normalized = (n - 1) / (numberKeyMax - 1);
              _value = normalizeSteppedValue(
                min + normalized * (max - min),
                min: min,
                max: max,
                step: step,
              );
            }
          },
          max: numberKeyMax,
          hintLabel: '1–$numberKeyMax',
          hintDescription: 'set value',
        );
  }

  _bindings =
      _bindings + KeyBindings.prompt(onCancel: () => _cancelled = true);

  if (extraBindings != null) {
    _bindings = _bindings + extraBindings;
  }

  final frame = FrameView(
    title: title,
    theme: theme,
    bindings: _bindings,
  );

  void renderFrame(RenderOutput out) {
    frame.render(out, (ctx) {
      render(ctx, _value, ratio);
    });
  }

  final runner = PromptRunner(hideCursor: true);
  final result = runner.runWithBindings(
    render: renderFrame,
    bindings: _bindings,
  );

  return (result == PromptResult.cancelled || _cancelled) ? initial : _value;
}