update method

  1. @override
(MultiSelectModel<T>, 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
(MultiSelectModel<T>, Cmd?) update(Msg msg) {
  if (msg is KeyMsg) {
    final key = msg.key;

    // Check for Ctrl+C
    if (key.ctrl && key.runes.isNotEmpty && key.runes.first == 0x63) {
      return (this, Cmd.message(const SelectionCancelledMsg()));
    }

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

    if (keyMatches(key, [keyMap.confirm])) {
      final selectedItems = _selected.toList()..sort();
      final result = selectedItems.map((i) => _items[i]).toList();
      return (
        this,
        Cmd.message(MultiSelectionMadeMsg<T>(result, selectedItems)),
      );
    }

    if (keyMatches(key, [keyMap.toggle])) {
      _toggleSelection();
    } else if (keyMatches(key, [keyMap.toggleAll])) {
      _toggleAll();
    } else if (keyMatches(key, [keyMap.up])) {
      _cursorUp();
    } else if (keyMatches(key, [keyMap.down])) {
      _cursorDown();
    } else if (keyMatches(key, [keyMap.home])) {
      _goToStart();
    } else if (keyMatches(key, [keyMap.end])) {
      _goToEnd();
    } else if (keyMatches(key, [keyMap.pageUp])) {
      _pageUp();
    } else if (keyMatches(key, [keyMap.pageDown])) {
      _pageDown();
    }
  }

  return (this, null);
}