visit<T extends Widget> method

Element? visit<T extends Widget>({
  1. bool last = false,
  2. bool filter(
    1. T widget
    )?,
})

Visits the first T found and returns its Element.

If last is true, then it will return the last Element found. Visiting last is O(N), avoid using last = true.

Implementation

Element? visit<T extends Widget>({
  bool last = false,
  bool Function(T widget)? filter,
}) {
  Element? found;

  void visit(Element element) {
    if (element.widget is T && (filter?.call(element.widget as T) ?? true)) {
      found = element;
      if (!last) return;
    }
    element.visitChildren(visit);
  }

  (this as Element).visitChildren(visit);
  return found;
}