visitElementAfter method

  1. @override
void visitElementAfter(
  1. Element element
)

Called when an Element has been reached, after its children have been visited.

Will not be called if visitElementBefore returns false.

Implementation

@override
void visitElementAfter(md.Element element) {
  debugPrint('visitElementAfter $_level ${element.tag}');
  _level--;

  if (_elementList.isEmpty) return;
  var last = _elementList.last;
  _elementList.removeLast();
  var tempWidget;
  if (kTextTags.indexOf(element.tag) != -1) {
    if (_elementList.isNotEmpty &&
        kTextParentTags.indexOf(_elementList.last.tag) != -1) {
      // 内联标签处理
      _elementList.last.textSpans ??= [];
      _elementList.last.textSpans?.addAll(last.textSpans ?? []);
    } else {
      if (last.textSpans?.isNotEmpty ?? false) {
        tempWidget = RichText(
          text: TextSpan(
            children: last.textSpans,
            style: last.textStyle,
          ),
        );
      }
    }
  } else if ('li' == element.tag) {
    tempWidget = _resolveToLi(last);
  } else if ('pre' == element.tag) {
    tempWidget = _resolveToPre(last);
  } else if ('blockquote' == element.tag) {
    tempWidget = _resolveToBlockquote(last);
  } else if ('img' == element.tag) {
    if (_elementList.isNotEmpty &&
        (_elementList.last.textSpans?.isNotEmpty ?? false)) {
      _widgets.add(
        Padding(
          padding: padding,
          child: RichText(
            text: TextSpan(
              children: _elementList.last.textSpans,
              style: _elementList.last.textStyle,
            ),
          ),
        ),
      );
      _elementList.last.textSpans = null;
    }
    debugPrint(element.attributes.toString());
    //_elementList.clear();
    _widgets.add(
      Padding(
        padding: padding,
        child: widgetImage(element.attributes['src'] ?? ''),
      ),
    );
  } else if (last.widgets?.isNotEmpty ?? false) {
    if (last.widgets?.length == 1) {
      tempWidget = last.widgets?[0];
    } else {
      tempWidget = Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: last.widgets ?? [],
      );
    }
  }

  if (tempWidget != null) {
    if (_elementList.isEmpty) {
      _widgets.add(
        Padding(
          padding: padding,
          child: tempWidget,
        ),
      );
    } else {
      _elementList.last.widgets ??= [];
      if (tempWidget is List<Widget>) {
        _elementList.last.widgets?.addAll(tempWidget);
      } else {
        _elementList.last.widgets?.add(tempWidget);
      }
    }
  }
}