applyRecognizer function

List<InlineSpan> applyRecognizer(
  1. GestureRecognizer recognizer,
  2. List<InlineSpan> children
)

Re-attach recognizer to every text-bearing span in children.

Flutter fires a TextSpan.recognizer only on the span that OWNS the text under the pointer — a recognizer on a wrapper span whose text lives in children never receives a tap. Tag builders therefore can't just wrap: run the children through this and every leaf becomes tappable.

'link': (node, children) => TextSpan(
  style: underline,
  children: applyRecognizer(_linkTap, children),
),

WidgetSpans pass through untouched — their widgets own their own gestures. The recognizer's lifecycle stays the caller's (create in a State, dispose there).

Implementation

List<InlineSpan> applyRecognizer(
  GestureRecognizer recognizer,
  List<InlineSpan> children,
) => [
  for (final child in children)
    switch (child) {
      TextSpan() => TextSpan(
        text: child.text,
        style: child.style,
        recognizer: child.recognizer ?? recognizer,
        children:
            child.children == null
                ? null
                : applyRecognizer(recognizer, child.children!),
      ),
      _ => child,
    },
];