onMessageEdit method

void onMessageEdit(
  1. TextEditingController textEditingController, {
  2. List<User> mentionedUsers = const [],
})

onMessageEdit is a method which is used to check the pattern matches text is a String which is used to store the text being typed cursorPosition is an int which is used to store the cursor position

Implementation

void onMessageEdit(TextEditingController textEditingController,
    {List<User> mentionedUsers = const []}) {
  if (mentionCount.length == 10 || listItems.isNotEmpty) {
    CometChatUIEvents.hidePanel(composerId, CustomUIPosition.composerPreview);
  }
  mentionCount.clear();
  mentionedUsersMap.clear();
  resetMentionsTracker();

  String text = textEditingController.text;

  while (pattern!.hasMatch(text)) {
    var match = pattern!.firstMatch(text);
    if (match != null) {
      String uid = match.group(1)!;

      int index =
          mentionedUsers.indexWhere((userElement) => userElement.uid == uid);

      if (index != -1) {
        String mention = "@${mentionedUsers[index].name}";

        if (mentionedUsersMap.containsKey(mention)) {
          mentionedUsersMap[mention]!.add(mentionedUsers[index]);
        } else {
          mentionedUsersMap[mention] = [mentionedUsers[index]];
        }

        if (!mentionCount.contains(mentionedUsers[index].uid)) {
          mentionCount.add(mentionedUsers[index].uid);
        }

        text = text.replaceRange(match.start, match.end, mention);
      }
    }
  }

  textEditingController.text = text;
  updatePreviousText(textEditingController.text);
  textEditingController.selection = TextSelection(
    baseOffset: text.length,
    extentOffset: text.length,
  );
  lastCursorPos = text.length;
}