descendants method

Iterable<Component> descendants(
  1. {bool includeSelf = false,
  2. bool reversed = false}
)

Recursively enumerates all nested children.

The search is depth-first in preorder. In other words, it explores the first child completely before visiting the next sibling, and the root component is visited before its children.

This ordering of descendants is considered standard in Flame: it is the same order in which the components will normally be updated and rendered on every game cycle. The optional parameter reversed allows iterating through the same set of descendants in reverse order.

The Iterable produced by this method is "lazy", which means it will only traverse the component tree when required. This allows efficient chaining of various iterable methods, such as filtering, early stopping, folding, and so on -- see the documentation of the Iterable class for details.

Implementation

Iterable<Component> descendants({
  bool includeSelf = false,
  bool reversed = false,
}) sync* {
  if (includeSelf && !reversed) {
    yield this;
  }
  if (hasChildren) {
    final childrenIterable = reversed ? children.reversed() : children;
    for (final child in childrenIterable) {
      yield* child.descendants(includeSelf: true, reversed: reversed);
    }
  }
  if (includeSelf && reversed) {
    yield this;
  }
}