buildTextSpan method

TextSpan buildTextSpan({
  1. required BuildContext context,
  2. TextStyle? style,
  3. AttributeGestureMapper? gestureMapper,
})

Apply the attributes to the text in the segments and return the resulting TextSpan.

The gestureMapper argument serves to manage the lifetimes of the GestureRecognizers created to handle gesture callbacks of TextAttributeValues. The caller is expected to properly manage the lifetime of this object by calling AttributeGestureMapper.dispose when appropriate.

If gestureMapper is null, no gesture recognizers will be put on the spans.

Implementation

TextSpan buildTextSpan({
  required BuildContext context,
  TextStyle? style,
  AttributeGestureMapper? gestureMapper,
}) {
  final span = TextSpan(
    style: style,
    children: map((segment) {
      final spanText = segment.text;

      GestureRecognizer? spanRecognizer;

      final attrs = segment.attributes;
      final values = attrs.map((attr) => attr.resolve(context)).toList();

      final style = values.fold<TextStyle>(
        const TextStyle(),
        (style, v) => style.merge(v.style),
      );

      if (gestureMapper != null) {
        final spanRecognizers = <GestureRecognizer>[];
        for (final v in values.where((v) => v.hasGestures)) {
          final recognizer = gestureMapper._getRecognizer(v);
          spanRecognizers.add(recognizer);
        }

        if (spanRecognizers.length > 1) {
          throw ArgumentError(
              'Tried to have more than 1 gesture recognizers on a single span.');
        } else if (spanRecognizers.length == 1) {
          spanRecognizer = spanRecognizers.first;
        }
      }

      return TextSpan(
        text: spanText.string,
        style: style,
        recognizer: spanRecognizer,
      );
    }).toList(),
  );

  return span;
}