filter method

WidgetbookNode? filter(
  1. bool predicate(
    1. WidgetbookNode node
    )
)

Filters the sub-tree of this node for any node that matches predicate. If a node matches the predicate, it will be included, along with all its descendants, in the result.

Returns null if no node matches the predicate.

Implementation

WidgetbookNode? filter(
  bool Function(WidgetbookNode node) predicate,
) {
  if (predicate(this)) {
    return this;
  } else {
    final filteredChildren = children
        ?.map((child) => child.filter(predicate))
        .whereType<WidgetbookNode>()
        .toList();

    return filteredChildren == null || filteredChildren.isEmpty
        ? null
        : copyWith(
            children: filteredChildren,
          );
  }
}