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> children = [];
  String patternMatched = "";
  String? formatText;
  TextStyle? myStyle;
  text.splitMapJoin(
    pattern,
    onMatch: (Match match) {
      myStyle = map[match[0]!] ??
          map[map.keys.firstWhere(
            (e) {
              bool ret = false;
              RegExp(e).allMatches(text)
                ..forEach((element) {
                  if (element.group(0) == match[0]) {
                    patternMatched = e;
                    ret = true;
                    return;
                  }
                });
              return ret;
            },
          )];

      if (patternMatched == r"_(.*?)\_") {
        formatText = match[0]!.replaceAll("_", " ");
      } else if (patternMatched == r'\*(.*?)\*') {
        formatText = match[0]!.replaceAll("*", " ");
      } else if (patternMatched == "~(.*?)~") {
        formatText = match[0]!.replaceAll("~", " ");
      } else if (patternMatched == r'```(.*?)```') {
        formatText = match[0]!.replaceAll("```", "   ");
      } else {
        formatText = match[0];
      }
      children.add(TextSpan(
        text: formatText,
        style: style!.merge(myStyle),
      ));
      return "";
    },
    onNonMatch: (String text) {
      children.add(TextSpan(text: text, style: style));
      return "";
    },
  );

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