recalculateStyle method

void recalculateStyle({
  1. bool rebuildNested = false,
  2. bool forceRecalculate = false,
})

Implementation

void recalculateStyle(
    {bool rebuildNested = false, bool forceRecalculate = false}) {
  // Pseudo elements (::before/::after) are styled via their parent's
  // matched pseudo rules. A full recalc using the standard element
  // pipeline would discard those properties (only defaults/inline apply).
  // Skip full recalc here to preserve pseudo-specific styles, which are
  // refreshed via markBefore/AfterPseudoElementNeedsUpdate on the parent.
  if (this is PseudoElement) {
    // Still flush any pending inline or merged properties if present.
    style.flushPendingProperties();
    return;
  }
  // Always update CSS variables even for display:none elements when rebuilding nested
  bool shouldUpdateCSSVariables =
      rebuildNested && renderStyle.display == CSSDisplay.none;

  if (forceRecalculate ||
      renderStyle.display != CSSDisplay.none ||
      shouldUpdateCSSVariables) {
    // Diff style.
    ElementCSSStyleDeclaration newStyle = ElementCSSStyleDeclaration();
    applyStyle(newStyle);
    style.merge(newStyle);
    final bool hasInheritedPendingProperty = style.hasInheritedPendingProperty;
    // Always flush after a recalc. Pending properties may have already been
    // queued (e.g. via setInlineStyle) and merge() can be a no-op when the
    // queued values match the freshly computed ones.
    style.flushPendingProperties();

    if (rebuildNested || hasInheritedPendingProperty) {
      // Update children style.
      for (final Element child in children) {
        child.recalculateStyle(
            rebuildNested: rebuildNested, forceRecalculate: forceRecalculate);
      }
    }
  }
}