buildTextSpan static method

TextSpan buildTextSpan(
  1. BuildContext? context,
  2. String rawText, {
  3. Color? color,
  4. double? fontSize,
  5. LetterSpacing? letterSpacing,
  6. LineHeight? lineHeight,
  7. FontName? fontName,
  8. TextDecorationEnum? textDecoration,
  9. List<Effect>? effects,
  10. required BaseNode? node,
  11. required List<VariableData> variablesOverrides,
  12. required NullSubstitutionMode nullSubstitutionMode,
  13. required bool replaceVariableWithSymbol,
  14. bool hasMissingFont = false,
  15. TapGestureRecognizer? tapGestureRecognizer,
})

Implementation

static TextSpan buildTextSpan(
  BuildContext? context,
  String rawText, {
  // Text properties.
  Color? color,
  double? fontSize,
  LetterSpacing? letterSpacing,
  LineHeight? lineHeight,
  FontName? fontName,
  TextDecorationEnum? textDecoration,
  List<Effect>? effects,

  // Additional properties.
  required BaseNode? node,
  required List<VariableData> variablesOverrides,
  required NullSubstitutionMode nullSubstitutionMode,
  required bool replaceVariableWithSymbol,
  bool hasMissingFont = false,
  TapGestureRecognizer? tapGestureRecognizer,
}) {
  String characters = rawText;

  final TextStyle style = switch (hasMissingFont) {
    false => retrieveTextStyle(
        fontSize: fontSize,
        fontName: fontName,
        textDecoration: textDecoration,
        lineHeight: lineHeight,
        letterSpacing: letterSpacing,
        color: color,
        effects: effects ?? (node is BlendMixin ? node.effects : const []),
      ),
    true => TextStyle(
        color: Colors.red,
        fontSize: fontSize,
        fontWeight: FontWeight.bold,
        decorationColor: Colors.yellow,
        decoration: TextDecoration.underline,
      ),
  };

  // Replace with fx symbol if required.
  if (replaceVariableWithSymbol) {
    final spans = characters
        .splitMap(
          variableSyntaxIdentifierRegex,
          onMatch: (match) =>
              VariableSpan(variable: characters, style: style),
          onNonMatch: (text) => TextSpan(text: text, style: style),
        )
        .toList();

    if (spans.length == 1) return spans.first;

    return TextSpan(children: spans, style: style);
  } else if (context != null) {
    // Substitute variables.
    characters = PropertyValueDelegate.substituteVariables(
      rawText,
      nullSubstitutionMode: nullSubstitutionMode,
      scopedValues: ScopedValues.of(
        context,
        variablesOverrides: variablesOverrides,
      ),
    );
  }

  return TextSpan(
    text: characters,
    style: style,
    recognizer: tapGestureRecognizer,
  );
}