buildTextSpan method

  1. @override
TextSpan buildTextSpan({
  1. BuildContext? context,
  2. TextStyle? style,
  3. bool? withComposing,
})
override

Builds TextSpan from current editing value.

By default makes text in composing range appear as underlined. Descendants can override this method to customize appearance of text.

Implementation

@override
TextSpan buildTextSpan(
    {BuildContext? context, TextStyle? style, bool? withComposing}) {
  var children = <InlineSpan>[];

  if (_pattern == null || _pattern == '()') {
    children.add(TextSpan(text: text, style: style));
  } else {
    text.splitMapJoin(
      RegExp('$_pattern'),
      onMatch: (Match match) {
        if (_allMentionWords.isNotEmpty) {
          final mention = _allMentionWords[match[0]!] ??
              _allMentionWords[_allMentionWords.keys.firstWhere((element) {
                final reg = RegExp(element);

                return reg.hasMatch(match[0]!);
              })]!;

          children.add(
            TextSpan(
              text: match[0],
              style: style!.merge(mention.style),
            ),
          );
        }

        return '';
      },
      onNonMatch: (String text) {
        children.add(TextSpan(text: text, style: style));
        return '';
      },
    );
  }

  return TextSpan(style: style, children: children);
}