selection property

SelectionModel<T> get selection
inherited

The selection model this container represents.

Implementation

SelectionModel<T> selection = SelectionModel.empty();
  1. @override
set selection (SelectionModel<T>? selection)
override

The selection model this container represents.

Implementation

@override
set selection(SelectionModel<T>? selection) {
  var localSelection = selection ?? SelectionModel.empty();
  super.selection = localSelection;
  activeModel.activateFirstItemByDefault =
      (isSingleSelect && accessibleItemActivation) ||
          (isMultiSelect && !accessibleItemActivation);

  if (isSingleSelect && localSelection.selectedValues.isNotEmpty) {
    _lastSelectedItem = localSelection.selectedValues.first;
    if (_isInitialized) {
      // Make sure input text is initialized correctly regardless of input
      // order. Specified input text should take precedence over selection
      // status.
      inputText = _renderWithItemRender(_lastSelectedItem);
    }
  }

  _selectionListener?.cancel();
  _selectionListener = localSelection.selectionChanges.listen((_) {
    // If the input fields shows the selected value then update it if the
    // selection changes or clear it if the selection model is empty.
    if (shouldClearInputOnSelection) {
      inputText = '';
    } else if (isSingleSelect) {
      var selectedItem = localSelection.selectedValues.isNotEmpty
          ? localSelection.selectedValues.first
          : null;
      // Make sure that the change was not caused by this component.
      if (_lastSelectedItem != selectedItem) {
        _lastSelectedItem = selectedItem;
        inputText = _renderWithItemRender(_lastSelectedItem);
      }
    }
    emitSelectionChange();
  });
}