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,
}) {
  final List<InlineSpan> textSpanChildren = <InlineSpan>[];
  final matchPairs = getTagMatchPairs(text);
  int position = 0;

  // For each pair of markers, add the text before the tag and the tag itself
  for (int i = 0; i < matchPairs.length; i = i + 1) {
    // Matches are not null because null values are corrected by the cursorController
    final (start as Match, end as Match) = matchPairs[i];
    final String textBeforeTag = text.substring(position, start.start);
    textSpanChildren.add(TextSpan(text: textBeforeTag, style: style));

    final String textInTag = text.substring(start.end, end.start);
    final tagPrompt = _parseTagPrompt(textInTag);
    final tagTextStyle = style?.merge(tagStyles[tagPrompt.prefix]) ??
        tagStyles[tagPrompt.prefix];
    textSpanChildren.add(TextSpan(
        text: '$tagStartMarker$textInTag$tagEndMarker', style: tagTextStyle));

    position = end.end;
  }

  // Finally, format the text after the last tag with the default style
  final String textAfterAllTags = text.substring(position, text.length);
  textSpanChildren.add(TextSpan(text: textAfterAllTags, style: style));

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