build method

  1. @override
TextSpan build(
  1. String data, {
  2. TextStyle? textStyle,
  3. SpecialTextGestureTapCallback? onTap,
})

Implementation

@override
TextSpan build(
  String data, {
  TextStyle? textStyle,
  SpecialTextGestureTapCallback? onTap,
}) {
  StringBuffer buffer = StringBuffer();
  if (kIsWeb) {
    return TextSpan(text: data, style: textStyle);
  }
  // if (allAtMap.isEmpty) {
  //   return TextSpan(text: data, style: textStyle);
  // }
  final List<InlineSpan> children = <InlineSpan>[];

  var regexEmoji = emojiFaces.keys
      .toList()
      .join('|')
      .replaceAll('[', '\\[')
      .replaceAll(']', '\\]');

  final list = [regexAt, regexEmoji];
  final pattern = '(${list.toList().join('|')})';
  final atReg = RegExp(regexAt);
  final emojiReg = RegExp(regexEmoji);

  data.splitMapJoin(
    RegExp(pattern),
    // RegExp(r"(@\S+\s)"),
    onMatch: (Match m) {
      late InlineSpan inlineSpan;
      String value = m.group(0)!;
      try {
        if (atReg.hasMatch(value)) {
          String id = value.replaceFirst("@", "").trim();
          if (allAtMap.containsKey(id)) {
            var name = allAtMap[id]!;
            // inlineSpan = ExtendedWidgetSpan(
            //   child: Text('@$name ', style: atStyle),
            //   style: atStyle,
            //   actualText: '$value',
            //   start: m.start,
            // );
            inlineSpan = SpecialTextSpan(
              text: '@$name ',
              actualText: '$value',
              start: m.start,
              style: atStyle,
            );
            buffer.write('@$name ');
          } else {
            inlineSpan = TextSpan(text: '$value', style: textStyle);
            buffer.write('$value');
          }
        } else if (emojiReg.hasMatch(value)) {
          inlineSpan = ImageSpan(
            ImageUtil.emojiImage(value),
            imageWidth: atStyle!.fontSize!,
            imageHeight: atStyle!.fontSize!,
            start: m.start,
            actualText: '$value',
          );
        }
        /*String id = value.replaceAll("@", "").trim();
      if (allAtMap.containsKey(id)) {
        var name = allAtMap[id]!;
        inlineSpan = ExtendedWidgetSpan(
          child: Text('@$name ', style: atStyle),
          style: atStyle,
          actualText: '$value',
          start: m.start,
        );
        buffer.write('@$name ');
      }*/
        else {
          /* inlineSpan = SpecialTextSpan(
          text: '${m.group(0)}',
          style: TextStyle(color: Colors.blue),
          start: m.start,
        );*/
          inlineSpan = TextSpan(text: '$value', style: textStyle);
          buffer.write('$value');
        }
      } catch (e) {
        print('error: $e');
      }
      children.add(inlineSpan);
      return "";
    },
    onNonMatch: (text) {
      children.add(TextSpan(text: text, style: textStyle));
      buffer.write(text);
      return '';
    },
  );
  if (null != atCallback) atCallback!(buffer.toString(), data);
  return TextSpan(children: children, style: textStyle);
}