buildTextSpan method

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

Builds a TextSpan from the current text, highlighting the matches for textPatternStyle.

Implementation

@override
TextSpan buildTextSpan({
  required BuildContext context,
  TextStyle? style,
  required bool withComposing,
}) {
  final pattern = textPatternStyle;
  if (pattern == null || pattern.isEmpty) {
    return super.buildTextSpan(
      context: context,
      style: style,
      withComposing: withComposing,
    );
  }

  return TextSpan(text: text, style: style).splitMapJoin(
    RegExp(pattern.keys.map((it) => it.pattern).join('|')),
    onMatch: (match) {
      final text = match[0]!;
      final key = pattern.keys.firstWhere((it) => it.hasMatch(text));
      return TextSpan(
        text: text,
        style: pattern[key]?.call(
          context,
          text,
        ),
      );
    },
  );
}