visitAll<T extends Widget> method
Visits all T widgets below this context. If T is absent, visits all.
Additionally returns a list of the Widgets found.
You can rebuild the widgets found. Native and private classes are ignored, making this very lightweight and fast.
Implementation
List<T> visitAll<T extends Widget>({
bool rebuild = false,
void Function(Widget parent, T widget)? onWidget,
void Function(Element parent, Element element)? onElement,
}) {
final list = <T>[];
var parent = this as Element;
bool ignoreType(Widget widget) {
if (widget.runtimeType.toString().startsWith('_')) return true;
if (widget.runtimeType.toString().startsWith('Cupertino')) return true;
return false;
}
void visit(Element element) {
if (element.widget is T) {
onElement?.call(parent, element);
onWidget?.call(parent.widget, element.widget as T);
if (!rebuild) list.add(element.widget as T);
if (rebuild && !ignoreType(element.widget)) {
list.add(element.widget as T);
element.markNeedsBuild();
}
}
parent = element;
element.visitChildren(visit);
}
parent.visitChildren(visit);
return list;
}