applySuggestion<T> method

void applySuggestion<T>({
  1. required Matcher matcher,
  2. required T suggestion,
})

Apply the selected suggestion from the matcher to the text field. This will replace the partial text with the suggestion.

Eg text field of "Hey @Ir", then applying will result in "Hey @Ironman"

Implementation

void applySuggestion<T>({
  required Matcher matcher,
  required T suggestion,
}) {
  assert(matcher.indexOfMatch != null && matcher.lengthOfMatch != null);

  final replacement = '${matcher.trigger}${matcher.displayProp(suggestion)} ';
  _controller.text = _controller.value.text.replaceRange(
    matcher.indexOfMatch!,
    matcher.indexOfMatch! + matcher.lengthOfMatch!,
    replacement,
  );

  _controller.selection = TextSelection.fromPosition(TextPosition(offset: matcher.indexOfMatch! + replacement.length));

  if (matcher.onSuggestionAdded != null) {
    matcher.onSuggestionAdded!(matcher.trigger, suggestion);
  }
}