MultiSelectable<K extends Key> class

A widget which simply enables multi-selection for items, these items can be SelectableItem, or other customizable Widget.

Note that items are distinguished through Key, and these keys are unnecessary to be unique. Also note that this widget only contains related settings about multi-selection, and have no display influence on child.

Example:

final _controller = MultiSelectableController<ValueKey<int>>();

// use SelectableItem directly
MultiSelectable<ValueKey<int>>(
  controller: _controller,
  stateSetter: () => mountedSetState(() {}),
  exitWhenNoSelect: true,
  maxCount: 6,
  child: ListView.builder(
    itemCount: 20,
    itemBuilder: (c, i) => SelectableItem<ValueKey<int>>(
      key: ValueKey<int>(i),
      builder: (_, key, tip) => Card(
        color: tip.isNormal ? Colors.white : tip.isSelected ? Colors.yellow : Colors.grey[200],
        child: ListTile(
          title: Text('Item ${i + 1}'),
          selected: tip.selected,
          onTap: () => !tip.isNormal ? tip.toToggle?.call() : /* ... */,
          onLongPress: tip.isNormal ? () => _controller.enterMultiSelectionMode(alsoSelect: [key]) : null,
        ),
      ),
    ),
  ),
)

// use SelectableCheckBoxItem
MultiSelectable<ValueKey<int>>(
  controller: _controller,
  stateSetter: () => mountedSetState(() {}),
  exitWhenNoSelect: true,
  maxCount: 6,
  child: ListView.builder(
    itemCount: 20,
    itemBuilder: (c, i) => SelectableCheckBoxItem<ValueKey<int>>(
      key: ValueKey<int>(i),
      useFullRipple: true,
      builder: (_, key, tip) => Card(
        child: ListTile(
          title: Text('Item ${i + 1}'),
          selected: tip.selected,
          onTap: () { /* ... */ },
          onLongPress: tip.isNormal ? () => _controller.enterMultiSelectionMode(alsoSelect: [key]) : null,
        ),
      ),
    ),
  ),
)
Inheritance

Constructors

MultiSelectable({Key? key, required MultiSelectableController<K> controller, required VoidCallback stateSetter, required Widget child, bool exitWhenNoSelect = true, void onModeChanged(bool newInMultiSelectionMode)?, void onSelectChanged(Set<K> allSelectedItems, Iterable<K> selectedItems, Iterable<K> unselectedItems)?, bool itemSelectable(K item)?, void onInvalidSelected(K invalidKey)?, int? maxSelectableCount, void onMaxCountReached(K exceedKey)?})
const

Properties

child Widget
The widget below this widget in the tree, typically is a Scrollable widget.
final
controller MultiSelectableController<K>
The controller of this widget, which can be used to control multi-selection.
final
exitWhenNoSelect bool
The flag to exit multi-selection mode when there is no item selected, defaults to true.
final
hashCode int
The hash code for this object.
no setterinherited
itemSelectable → (bool Function(K item)?)
The predicate function for checking whether item is selectable, defaults to make all items selectable. Note that this checking is happened before maxSelectableCount.
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
maxSelectableCount int?
The max selectable count for multi-selection, defaults to null, which means no checking. Note that this checking is happened after itemSelectable.
final
onInvalidSelected → (void Function(K invalidKey)?)
The callback that will be invoked when an invalid item is requested to select, checking through itemSelectable.
final
onMaxCountReached → (void Function(K exceedKey)?)
The callback that will be invoked when selection count reaches to maxSelectableCount.
final
onModeChanged → (void Function(bool newInMultiSelectionMode)?)
The callback that will be invoked when enter or exit multi-selection mode.
final
onSelectChanged → (void Function(Set<K> allSelectedItems, Iterable<K> selectedItems, Iterable<K> unselectedItems)?)
The callback that will be invoked when some new items are selected or unselected.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stateSetter VoidCallback
The state setter of parent widget.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<MultiSelectable<K>>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

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

Static Methods

of<K extends Key>(BuildContext context) MultiSelectable<K>?
Returns the MultiSelectable most closely associated with the given context.