update method

  1. @override
(NumberInputModel, Cmd?) update(
  1. Msg msg
)
override

Updates the component state in response to a message.

Returns the updated component (often this) and an optional command.

Implementation

@override
(NumberInputModel, Cmd?) update(Msg msg) {
  if (msg is! KeyMsg) return (this, null);

  final key = msg.key;

  if (keyMatches(key, [keyMap.cancel])) {
    return (this, Cmd.message(const NumberCancelledMsg()));
  }

  if (keyMatches(key, [keyMap.submit])) {
    final err = _validateInput(_rawInput);
    if (err != null) {
      _error = err;
      return (this, null);
    }
    final parsed = num.tryParse(_rawInput);
    if (parsed == null) {
      _error = 'Must be a number.';
      return (this, null);
    }
    _error = null;
    return (this, Cmd.message(NumberSubmittedMsg(parsed)));
  }

  if (keyMatches(key, [keyMap.increment])) {
    _adjustValue(1);
    _error = null;
    return (this, null);
  }

  if (keyMatches(key, [keyMap.decrement])) {
    _adjustValue(-1);
    _error = null;
    return (this, null);
  }

  if (keyMatches(key, [keyMap.deleteBackward])) {
    if (_rawInput.isNotEmpty) {
      _rawInput = _dropLastGrapheme(_rawInput);
      _error = null;
    }
    return (this, null);
  }

  // Accept digit, minus sign, and decimal point characters.
  if (key.runes.isNotEmpty) {
    final ch = String.fromCharCodes(key.runes);
    if (_isValidInputChar(ch)) {
      _rawInput += ch;
      _error = null;
    }
    return (this, null);
  }

  return (this, null);
}