addSelectedItem method

void addSelectedItem({
  1. required T item,
  2. bool fromSelectBox = false,
  3. bool fromEditBox = false,
  4. bool fromDragDrop = false,
})

Implementation

void addSelectedItem(
    {required T item,
    bool fromSelectBox = false,
    bool fromEditBox = false,
    bool fromDragDrop = false}) {
  if (!fromDragDrop &&
      settings.selectionSettings.selectionMode ==
          MhItemsViewSelectionModes.none) {
    return;
  }

  if (settings.selectionSettings.selectionMode ==
      MhItemsViewSelectionModes.single) {
    // SingleSelection
    if (selectedItems.containsKey(item)) {
      clearSelectedItems();
    } else {
      clearSelectedItems();
      addSelectedItemInner(item);
      orderSelectedItems();
    }
  } else {
    // Multi Selection
    var ctrlIsPressed = HardwareKeyboard.instance.logicalKeysPressed.any(
        (i) =>
            i == LogicalKeyboardKey.controlLeft ||
            i == LogicalKeyboardKey.controlRight ||
            i == LogicalKeyboardKey.control);
    var shiftIsPressed = HardwareKeyboard.instance.logicalKeysPressed.any(
        (i) =>
            i == LogicalKeyboardKey.shiftLeft ||
            i == LogicalKeyboardKey.shiftRight ||
            i == LogicalKeyboardKey.shift);

    if (fromEditBox) {
      if (isSelected(item)) {
        return;
      }
      shiftIsPressed = false;
    }

    if (shiftIsPressed) {
      if (ctrlIsPressed) {
        //Just add the item
        selectedItems[item] = item;
        if (!selectedItems.containsKey(item)) {
          addSelectedItemInner(item);
          orderSelectedItems();
        }
      } else {
        if (selectedItemsOrderedList.isEmpty) {
          //Here we just select the item
          if (!selectedItems.containsKey(item)) {
            addSelectedItemInner(item);
            orderSelectedItems();
          }
        } else {
          //Here we have to select all items from last selected item to this item
          var lastSelectedItem = selectedItemsOrderedList.last;
          var indexOfLastSelectedItem =
              filteredItemsSource.indexOf(lastSelectedItem);
          var indexOfSelectedItem = filteredItemsSource.indexOf(item);
          if (indexOfLastSelectedItem < indexOfSelectedItem) {
            for (var i = indexOfLastSelectedItem;
                i <= indexOfSelectedItem;
                i++) {
              var itemToSelect = filteredItemsSource[i];
              if (!selectedItems.containsKey(itemToSelect)) {
                addSelectedItemInner(itemToSelect);
              }
            }
            orderSelectedItems();
          } else {
            for (var i = indexOfLastSelectedItem;
                i >= indexOfSelectedItem;
                i--) {
              var itemToSelect = filteredItemsSource[i];
              if (!selectedItems.containsKey(itemToSelect)) {
                addSelectedItemInner(itemToSelect);
              }
            }
            orderSelectedItems();
          }
        }
      }
    } else {
      if (ctrlIsPressed || fromSelectBox) {
        //Add this item
        if (!selectedItems.containsKey(item)) {
          addSelectedItemInner(item);
        }
      } else {
        //We select only this item
        clearSelectedItems();
        addSelectedItemInner(item);
      }
      orderSelectedItems();
    }
  }
}