selectWith method

void selectWith(
  1. TreeNodeId id, {
  2. bool toggle = false,
  3. bool range = false,
})

Pointer/keyboard selection honouring modifier keys and selectionMode: • plain → select only id (resets the set) • toggle → Ctrl/⌘-click: add/remove id, keep the rest (multi only) • range → Shift-click: select the contiguous visible range from the anchor to id (multi only) Falls back to single behaviour when selectionMode isn't multi.

Implementation

void selectWith(TreeNodeId id, {bool toggle = false, bool range = false}) {
  final n = node(id);
  if (n == null || !n.selectable || selectionMode == TreeSelectionMode.none) return;
  if (selectionMode == TreeSelectionMode.single) {
    select(id);
    return;
  }
  if (range && _anchor != null) {
    final rows = visibleRows();
    final a = rows.indexWhere((r) => r.node.id == _anchor);
    final b = rows.indexWhere((r) => r.node.id == id);
    if (a >= 0 && b >= 0) {
      final lo = a < b ? a : b, hi = a < b ? b : a;
      _selection.clear();
      for (var i = lo; i <= hi; i++) {
        if (rows[i].node.selectable) _selection.add(rows[i].node.id);
      }
    }
  } else if (toggle) {
    if (_selection.contains(id)) {
      _selection.remove(id);
    } else {
      _selection.add(id);
    }
    _anchor = id;
  } else {
    _selection
      ..clear()
      ..add(id);
    _anchor = id;
  }
  _selected = id;
  _focused = id;
  notifyListeners();
}