TreeController<T extends Object> class

A controller used to dynamically manage the state of a tree.

Whenever this controller notifies its listeners any attached tree views will assume that the tree structure changed in some way and will rebuild their internal flat representaton of the tree, showing/hiding the updated nodes (if any).

Usage:

class Node {
  Node(this.children);
  List<Node> children;
}

final TreeController<Node> treeController = TreeController<Node>(
  roots: <Node>[
    Node(<Node>[]),
  ],
  childrenProvider: (Node node) => node.children,
);

The default implementations of getExpansionState and setExpansionState use a Set to manage the expansion state of tree nodes as follows:

Those methods can be overridden to use other data structures if desired. Example:

class Node {
  bool isExpanded = false;
}

class MyTreeController extends TreeController<Node> {
  @override
  bool getExpansionState(Node node) => node.isExpanded;

  // Do not call `notifyListeners` from this method as it is called many
  // times recursively in cascading operations.
  @override
  void setExpansionState(Node node, bool expanded) {
    node.isExpanded = expanded;
  }
}
Mixed in types

Constructors

TreeController({required Iterable<T> roots, required ChildrenProvider<T> childrenProvider})
Creates a TreeController.

Properties

areAllRootsCollapsed bool
Whether all root nodes of this tree are collapsed.
no setter
areAllRootsExpanded bool
Whether all root nodes of this tree are expanded.
no setter
childrenProvider ChildrenProvider<T>
A callback used when building the flat representation of the tree to get the direct children of the tree node passed to it.
final
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
isTreeCollapsed bool
Whether all nodes of this tree are collapsed.
no setter
isTreeExpanded bool
Whether all nodes of this tree are expanded.
no setter
roots Iterable<T>
The roots of the tree.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
breadthFirstSearch({Iterable<T>? startingNodes, DescendCondition<T> descendCondition = alwaysReturnsTrue, ReturnCondition<T> returnCondition = alwaysReturnsFalse, Visitor<T>? onTraverse}) → T?
Traverses the subtrees of startingNodes in breadth first order. If startingNodes is not provided, roots will be used instead.
collapse(T node) → void
Sets the expansion state of node to false, then calls rebuild.
collapseAll() → void
Collapses all nodes of this tree recursively.
collapseCascading(Iterable<T> nodes) → void
Traverses the subtrees of nodes in depth first order collapsing every visited node, then calls rebuild.
defaultDescendCondition(TreeEntry<T> entry) bool
The default DescendCondition used by depthFirstTraversal.
depthFirstTraversal({required Visitor<TreeEntry<T>> onTraverse, DescendCondition<TreeEntry<T>>? descendCondition, TreeEntry<T>? rootEntry}) → void
Traverses the subtrees of roots creating TreeEntry instances for each visited node.
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
override
expand(T node) → void
Sets the expansion state of node to true, then calls rebuild.
expandAll() → void
Expands all nodes of this tree recursively.
expandAncestors(T node, ParentProvider<T> parentProvider) → void
Walks up the ancestors of node setting their expansion state to true. Note: node is not expanded by this method.
expandCascading(Iterable<T> nodes) → void
Traverses the subtrees of nodes in depth first order expanding every visited node, then calls rebuild.
getExpansionState(T node) bool
The current expansion state of node.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
rebuild() → void
Notify listeners that the tree structure changed in some way.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
setExpansionState(T node, bool expanded) → void
Updates the expansion state of node to the value of expanded.
toggleExpansion(T node) → void
Updates the expansion state of node to the opposite state, then calls rebuild.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited