getAttributedText method

  1. @override
List<AttributedText> getAttributedText(
  1. String text,
  2. CometChatTheme theme,
  3. BubbleAlignment? alignment, {
  4. List<AttributedText>? existingAttributes,
  5. dynamic onTap(
    1. String
    )?,
  6. bool forConversation = false,
})
override

getAttributedText is a function which is used to get the attributed text which is used to style the text in the message bubble and conversation subtitle

Implementation

@override
List<AttributedText> getAttributedText(
    String text, CometChatTheme theme, BubbleAlignment? alignment,
    {List<AttributedText>? existingAttributes,
    Function(String)? onTap,
    bool forConversation = false}) {
  List<AttributedText> attributedTexts = [];
  final matches = pattern!.allMatches(text);
  List<User> mentionedUsers = message?.mentionedUsers ?? [];

  attributedTexts = matches.map((match) {
    int start = match.start;
    int end = match.end;
    int userIndex =
        mentionedUsers.indexWhere((element) => element.uid == match.group(1));

    String underlyingText = userIndex != -1
        ? "@${mentionedUsers[userIndex].name}"
        : match.group(0) ?? '';
    bool isLoggedInUser = userIndex != -1 &&
        mentionedUsers[userIndex].uid == CometChatUIKit.loggedInUser?.uid;

    return AttributedText(
        start: start,
        end: end,
        underlyingText: underlyingText,
        style: getMessageBubbleTextStyle(theme, alignment,
            isLoggedInUser: isLoggedInUser, forConversation: forConversation),
        onTap: (text) {
          if (onMentionTap != null) {
            onMentionTap!(text, mentionedUsers[userIndex], message: message);
          }
        });
  }).toList();

  if (existingAttributes != null && existingAttributes.isNotEmpty) {
    return mergeAttributedText(attributedTexts, existingAttributes);
  } else {
    return attributedTexts;
  }
}