removeChip method

void removeChip(
  1. T chip
)

Removes the specified chip from the controller.

Implementation

void removeChip(T chip) {
  int? chipIndex;
  _chipMap.forEach((key, value) {
    if (value == chip) {
      chipIndex = key;
    }
  });
  if (chipIndex != null) {
    String text = value.text;
    StringBuffer buffer = StringBuffer();
    int currentCursorOffset = value.selection.baseOffset;
    int newCursorOffset = currentCursorOffset;

    for (int i = 0; i < text.length; i++) {
      int codeUnit = text.codeUnitAt(i);
      if (codeUnit == _chipStart + chipIndex!) {
        // Found the chip to remove
        // Adjust cursor position if it's after the removed chip
        if (currentCursorOffset > i) {
          newCursorOffset = currentCursorOffset - 1;
        }
        // skip this code unit
        continue;
      }
      buffer.writeCharCode(codeUnit);
    }
    super.value = value.copyWith(
      text: buffer.toString(),
      selection: TextSelection.collapsed(offset: newCursorOffset),
    );
    _chipMap.remove(chipIndex);
  }
}