buildTextSpan method

  1. @override
TextSpan buildTextSpan({
  1. required BuildContext context,
  2. TextStyle? style,
  3. required 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({required BuildContext context, TextStyle? style, required bool withComposing}) {
  if (matchers.isEmpty) {
    return TextSpan(
      text: text,
      style: style,
    );
  }

  var widgets = List<InlineSpan>.empty(growable: true);

  text.splitMapJoin(
    _combinedRegex,
    onMatch: (Match match) {
      final matcher = matchers.firstWhere((m) => RegExp(m.regexPattern).hasMatch(match[0]!));

      widgets.add(
        TextSpan(
          text: match[0],
          style: style!.merge(matcher.style),
        ),
      );

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

      return '';
    },
  );

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