update method
Updates the component state in response to a message.
Returns the updated component (often this) and an optional command.
Implementation
@override
(SuggestModel, Cmd?) update(Msg msg) {
if (msg is! KeyMsg) return (this, null);
final key = msg.key;
// Cancel
if (keyMatches(key, [keyMap.cancel])) {
return (this, Cmd.message(const SuggestCancelledMsg()));
}
// Accept
if (keyMatches(key, [keyMap.accept])) {
final value = _acceptedValue();
return (this, Cmd.message(SuggestSubmittedMsg(value)));
}
// Navigate up / down
if (keyMatches(key, [keyMap.moveDown])) {
_moveHighlight(1);
return (this, null);
}
if (keyMatches(key, [keyMap.moveUp])) {
_moveHighlight(-1);
return (this, null);
}
if (keyMatches(key, [keyMap.moveFirst])) {
if (_highlighted >= 0) {
_highlighted = 0;
_firstVisible = 0;
}
return (this, null);
}
if (keyMatches(key, [keyMap.moveLast])) {
if (_highlighted >= 0) {
final m = matches;
if (m.isNotEmpty) {
_highlighted = m.length - 1;
_firstVisible = (m.length - scroll).clamp(0, m.length - 1);
}
}
return (this, null);
}
// Left / Right arrows clear the highlight but don't consume the key
// (future: cursor movement within the input)
if (keyMatches(key, [keyMap.moveCursorLeft])) {
_highlighted = -1;
if (_cursor > 0) _cursor--;
return (this, null);
}
if (keyMatches(key, [keyMap.moveCursorRight])) {
_highlighted = -1;
final graphemes = uni.graphemes(_rawInput).toList();
if (_cursor < graphemes.length) _cursor++;
return (this, null);
}
// Backspace
if (keyMatches(key, [keyMap.deleteBackward])) {
_highlighted = -1;
if (_rawInput.isNotEmpty) {
final gs = uni.graphemes(_rawInput).toList();
if (_cursor > 0) {
gs.removeAt(_cursor - 1);
_rawInput = gs.join();
_cursor--;
}
}
_firstVisible = 0;
return (this, null);
}
// Character input
if (key.runes.isNotEmpty) {
final ch = String.fromCharCodes(key.runes);
_highlighted = -1;
final gs = uni.graphemes(_rawInput).toList();
gs.insert(_cursor, ch);
_rawInput = gs.join();
_cursor++;
_firstVisible = 0;
return (this, null);
}
return (this, null);
}