TextRenderer constructor

TextRenderer(
  1. String rawHtml,
  2. RendererStyle rendererDecoration,
  3. int? maxLength,
  4. int? maxLines,
  5. List<RichTextPlaceholder> placeholders,
  6. String placeholderMarker,
  7. bool ignoreLinebreaks,
)

recursively parses the richtext-tree

Implementation

TextRenderer(
  this.rawHtml,
  this.rendererDecoration,
  this.maxLength,
  this.maxLines,
  this.placeholders,
  this.placeholderMarker,
  this.ignoreLinebreaks,
) {
  _initFontStyles();

  NodeV2 root = Parser().parse(rawHtml);
  _proccessNode(root);

  int textLength = 0;
  bool full = false;

  for (int i = 0; i < _flattenedNodes.length; i++) {
    _TextNode node = _flattenedNodes[i];

    String nodeText = node.text;
    if (maxLength != null && textLength + nodeText.length > maxLength!) {
      nodeText = nodeText.substring(
              0, min(maxLength! - textLength, nodeText.length)) +
          rendererDecoration.overflowIndicator;
      full = true;
    }

    nodeText = Parser.replaceVariables(
      nodeText,
      placeholders: placeholders,
      placeholderMarker: placeholderMarker,
    );

    // linebreak before the new text
    if (!ignoreLinebreaks && _currentParagraph.length > 0) {
      if (node.invokesNewlineBefore) _performLinebreak();
    }

    GestureRecognizer? linkTapRecognizer;

    if (node.linkTarget != null) {
      final String target = node.linkTarget!;

      linkTapRecognizer = TapGestureRecognizer()
        ..onTap = () {
          launchUrl(Uri.parse(target));
        };
    }

    _currentParagraph.add(
      TextSpan(
        text: nodeText,
        style: node.style,
        recognizer: linkTapRecognizer,
      ),
    );
    textLength += nodeText.length;

    // linebreak before the new text
    if (!ignoreLinebreaks && _currentParagraph.length > 0) {
      if (node.invokesNewlineAfter) _performLinebreak();
    }

    if (full) break;
  }

  _performLinebreak();
}