getDetectedTextSpanWithExtraChild static method

TextSpan getDetectedTextSpanWithExtraChild({
  1. required TextStyle decoratedStyle,
  2. required TextStyle basicStyle,
  3. required String source,
  4. required RegExp detectionRegExp,
  5. dynamic onTap(
    1. String
    )?,
  6. bool decorateAtSign = false,
  7. List<InlineSpan>? children,
})

Implementation

static TextSpan getDetectedTextSpanWithExtraChild(
    {required TextStyle decoratedStyle,
    required TextStyle basicStyle,
    required String source,
    required RegExp detectionRegExp,
    Function(String)? onTap,
    bool decorateAtSign = false,
    List<InlineSpan>? children}) {
  final detections = source.toDetections(
    detectedStyle: decoratedStyle,
    textStyle: basicStyle,
    detectionRegExp: detectionRegExp,
  );
  if (detections.isEmpty) {
    // return TextSpan(text: source, style: basicStyle);
    return TextSpan(
      style: basicStyle,
      text: source,
      children: children,
    );
  } else {
    detections.sort();
    List<InlineSpan> span = detections
        .asMap()
        .map(
          (index, item) {
            TapGestureRecognizer? recognizer;
            final decoration = detections[index];
            if (decoration.style == decoratedStyle && onTap != null) {
              recognizer = TapGestureRecognizer()
                ..onTap = () {
                  onTap(decoration.range.textInside(source).trim());
                };
            }
            return MapEntry(
              index,
              TextSpan(
                style: item.style,
                text: item.range.textInside(source),
                recognizer: (onTap == null) ? null : recognizer,
              ),
            );
          },
        )
        .values
        .toList();

    span.addAll(children!);

    return TextSpan(children: span);
  }
}