buildSpans method

  1. @visibleForTesting
List<InlineSpan> buildSpans(
  1. BuildContext context,
  2. List<(List<String>, String)> substrings
)

Implementation

@visibleForTesting
List<InlineSpan> buildSpans(BuildContext context, List<(List<String> tokens, String content)> substrings) {
  final List<InlineSpan> spans = [];
  if (substrings.isEmpty) {
    return [TextSpan(style: defaultStyle ?? DefaultTextStyle.of(context).style, text: text)];
  }
  for (final (tokens, content) in substrings) {
    TextStyle style = defaultStyle ?? DefaultTextStyle.of(context).style;
    List<VoidCallback> tapHandlers = [];
    if (tokens.isEmpty) {
      spans.add(TextSpan(style: style, text: content));
      continue;
    }
    for (final token in tokens) {
      if (defaultTokens.containsKey(token)) {
        style = style.merge(defaultTokens[token]!);
      }
      if (colors.containsKey(token)) {
        style = style.copyWith(color: colors[token]);
      }
      if (styles.containsKey(token)) {
        style = style.merge(styles[token]!);
      }
      if (actions.containsKey(token)) {
        tapHandlers.add(actions[token]!);
      }
    }
    final recognizer = TapGestureRecognizer()
      ..onTap = () {
        for (final handler in tapHandlers) {
          handler();
        }
      };
    final span = TextSpan(style: style, text: content, recognizer: recognizer);
    spans.add(span);
  }
  return spans;
}