getDetectedTextSpan static method

TextSpan getDetectedTextSpan({
  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,
})

Returns textSpan with detected text

Used in DetectableText

Implementation

static TextSpan getDetectedTextSpan({
  required TextStyle decoratedStyle,
  required TextStyle basicStyle,
  required String source,
  required RegExp detectionRegExp,
  Function(String)? onTap,
  bool decorateAtSign = false,
}) {
  final detections = source.toDetections(
    detectedStyle: decoratedStyle,
    textStyle: basicStyle,
    detectionRegExp: detectionRegExp,
  );
  if (detections.isEmpty) {
    return TextSpan(text: source, style: basicStyle);
  } else {
    detections.sort();
    final 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();

    return TextSpan(children: span);
  }
}