appendChip method

void appendChip(
  1. T chip
)

Appends a chip at the end of the chip sequence.

Implementation

void appendChip(T chip) {
  // append chip at the last chip position
  // note: chip position is not always in order
  // sometimes theres chip and then text and then chip
  // so we need to find the last chip position
  String text = value.text;
  int chipIndex = _nextAvailableChipIndex;
  int lastChipIndex = -1;
  for (int i = text.length - 1; i >= 0; i--) {
    int codeUnit = text.codeUnitAt(i);
    if (codeUnit >= _chipStart && codeUnit <= _chipEnd) {
      lastChipIndex = i;
      break;
    }
  }
  String newText = text.replaceRange(lastChipIndex + 1, lastChipIndex + 1,
      String.fromCharCode(_chipStart + chipIndex));
  super.value = value.copyWith(
    text: newText,
    selection: TextSelection.collapsed(offset: lastChipIndex + 2),
  );
  _chipMap[chipIndex] = chip;
}