afterElement method

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

Called after an element's children have all been visited.

Override only if you need depth or nesting tracking (e.g. scroll-axis stack, depth counter). Default is a no-op.

Implementation

@override
void afterElement(Element element) {
  final subtreeSize = _subtreeSizeStack.removeLast();
  if (_subtreeSizeStack.isNotEmpty) {
    _subtreeSizeStack.last += subtreeSize + 1;
  }

  // Only the element that owns a render object judges it. Wrapper
  // elements (a Material, a Builder) resolve `renderObject` to the same
  // descendant render object and would report it once more each, with
  // a subtree one larger per hop.
  if (element is! RenderObjectElement) return;
  final ro = element.renderObject;

  // Direct type checks — no runtimeType.toString() allocation.
  // Excludes RenderPhysicalModel/Shape (Card, Material) — these are
  // normal and hardware-accelerated in profile mode.
  // Note: `is RenderOpacity` correctly excludes RenderAnimatedOpacity
  // (which extends RenderProxyBox, not RenderOpacity) — the previous
  // contains('RenderOpacity') matched it as a false positive.
  String? typeName;
  double? backdropSigma;
  String? chromeOwner;
  if (ro is RenderOpacity) {
    final val = ro.opacity;
    if (val >= 1.0 || val <= 0.0) return; // no-op or short-circuit
    typeName = 'RenderOpacity';
  } else if (ro is RenderClipPath) {
    // The clip a transparency Material builds for its own shape is
    // framework-owned (buttons, chips); only user ClipPaths count.
    if (isMaterialOwnClip(element)) return;
    typeName = 'RenderClipPath';
  } else if (ro is RenderBackdropFilter) {
    // A disabled filter draws nothing (an opaque Cupertino bar keeps its
    // BackdropFilter with `enabled: false`).
    if (!ro.enabled) return;
    final widget = element.widget;
    if (widget is BackdropFilter) {
      backdropSigma = maxBlurSigma(widget);
      if (backdropSigma != null && backdropSigma <= _lowSigmaThreshold) {
        return;
      }
      chromeOwner = cupertinoBlurOwner(element);
    }
    typeName = 'RenderBackdropFilter';
  } else if (ro is RenderShaderMask) {
    typeName = 'RenderShaderMask';
  } else if (element.widget is ColorFiltered) {
    // ColorFiltered uses a private _ColorFilterRenderObject — can't use
    // `is` check on the render object. Check the widget type instead.
    typeName = 'RenderColorFiltered';
  }

  if (typeName == null) return;

  if (subtreeSize > 5) {
    // Sigma-aware detail for BackdropFilter. The outline label names
    // the type before the highlight detail, so the detail leaves it out.
    String nodeDetail = '$typeName ($subtreeSize descendants)';
    String highlightDetail = '$subtreeSize descendants';
    IssueSeverity highlightSeverity = IssueSeverity.warning;

    if (backdropSigma != null) {
      final sigmaStr = backdropSigma.toStringAsFixed(1);
      nodeDetail = '$typeName ($subtreeSize descendants, σ=$sigmaStr)';
      highlightDetail = '$subtreeSize descendants (σ=$sigmaStr)';
      if (backdropSigma > _highSigmaThreshold) {
        highlightSeverity = IssueSeverity.critical;
      }
    }

    if (chromeOwner != null) {
      nodeDetail = '$nodeDetail, drawn by $chromeOwner';
      highlightDetail = '$highlightDetail, drawn by $chromeOwner';
      _blurChromeOwners.add(chromeOwner);
      _chromeNodeCount++;
    }

    _expensiveNodes.add(nodeDetail);
    final rect = getGlobalRect(ro);
    if (rect != null) {
      _highlights.add(
        WidgetHighlight(
          rect: rect,
          renderObject: ro,
          occurrenceId: identityHashCode(element),
          stableId: 'expensive_gpu_nodes',
          widgetName: typeName, // known from type check — no toString()
          severity: highlightSeverity,
          detectorName: 'GPU',
          detail: highlightDetail,
        ),
      );
    }
  }
}