SelectionController class

Manages selection state for list and grid based views.

Why it exists

UI code often mixes navigation (which item is focused) with selection (which items are "picked"). SelectionController separates these responsibilities so list, grid, or focus navigators can stay dumb and reusable.

Features

  • Single- or multi-select modes via the same API
  • Toggle/select/deselect helpers that keep code expressive
  • selectAll, toggleAll, and invert for power-user shortcuts
  • Safe result extraction helpers that gracefully fallback to the focused row
  • Can be copied/cloned, making it easy to snapshot state

Single-select usage

final nav = ListNavigator(itemCount: items.length, maxVisible: 10);
final sel = SelectionController.single();

sel.toggle(nav.selectedIndex); // replaces the single selection
final selectedItem = sel.getSelected(
  items,
  fallbackIndex: nav.selectedIndex,
);

Multi-select usage

final nav = GridNavigator(itemCount: items.length, columns: 4);
final sel = SelectionController.multi();

sel.toggle(nav.focusedIndex); // add/remove the focused tile
sel.selectAll(items.length);  // convenience for Cmd+A style shortcuts
final picked = sel.getSelectedMany(items);

Key binding integration

KeyBindings.toggle(
  onToggle: () => sel.toggle(nav.selectedIndex),
  onSelectAll: () => sel.selectAll(items.length),
);
Available extensions

Constructors

SelectionController({bool multiSelect = false, Set<int>? initialSelection})
Creates a selection controller.
SelectionController.multi({Set<int>? initialSelection})
Creates a multi-selection controller.
factory
SelectionController.single({int? initialIndex})
Creates a single-selection controller.
factory

Properties

count int
Number of selected items.
no setter
hashCode int
The hash code for this object.
no setterinherited
isEmpty bool
Whether no items are selected.
no setter
isNotEmpty bool
Whether at least one item is selected.
no setter
multiSelect bool
Whether this controller allows multiple selections.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
selectedIndex int?
The single selected index (for single-select mode).
no setter
selectedIndices Set<int>
Returns an unmodifiable view of selected indices.
no setter

Methods

clear() → void
Clears all selections.
constrainTo(int itemCount) → void
Constrains selection to valid indices for the given item count.
copy() SelectionController
Creates a copy of this controller.
deselect(int index) → void
Deselects an index.
getSelected<T>(List<T> items, {int? fallbackIndex}) → T?
Gets the single selected item from a list.
getSelectedIndices({int? fallbackIndex}) List<int>
Gets the selected indices as a sorted list.
getSelectedMany<T>(List<T> items, {int? fallbackIndex}) List<T>
Gets the selected items from a list.
invert(int count) → void
Inverts the selection (selects unselected, deselects selected).
isFocusedSelected(int focusedIndex) bool

Available on SelectionController, provided by the SelectionControllerExt extension

Checks if the focused item is selected.
isSelected(int index) bool
Checks if an index is selected.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
select(int index) → void
Selects an index (adds to selection in multi-select, replaces in single).
selectAll(int count) → void
Selects all indices from 0 to count-1.
summary(int totalCount) String
Returns a summary string for display.
toggle(int index) → void
Toggles selection of an index.
toggleAll(int count) → void
Selects all if any are unselected, otherwise clears all.
toggleFocused(int focusedIndex) → void

Available on SelectionController, provided by the SelectionControllerExt extension

Toggles the currently focused item.
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

validatedIndices(Set<int>? indices, int itemCount) Set<int>?
Filters indices to only those within [0, itemCount).