visitAncestorElements static method

void visitAncestorElements(
  1. BuildContext context,
  2. ConditionalElementVisitor visitor
)

Walks the ancestor chain, starting with the parent of the build context's widget, invoking the argument for each ancestor until a LookupBoundary or the root is reached.

This method behaves exactly like BuildContext.visitAncestorElements, except it only walks the tree up to the closest LookupBoundary ancestor of the provided context. The root of the tree is treated as an implicit lookup boundary.

The callback is given a reference to the ancestor widget's corresponding Element object. The walk stops when it reaches the root widget or when the callback returns false. The callback must not return null.

This is useful for inspecting the widget tree.

Calling this method is relatively expensive (O(N) in the depth of the tree).

This method should not be called from State.deactivate or State.dispose because the element tree is no longer stable at that time. To refer to an ancestor from one of those methods, save a reference to the ancestor by calling visitAncestorElements in State.didChangeDependencies.

Implementation

static void visitAncestorElements(BuildContext context, ConditionalElementVisitor visitor) {
  context.visitAncestorElements((Element ancestor) {
    return visitor(ancestor) && ancestor.widget.runtimeType != LookupBoundary;
  });
}