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).

Make sure to define a parentProvider when using methods that depend on it, like expandAncestors and checkNodeHasAncestor, or indirectly depend on it like the drag and drop widgets TreeDraggable and TreeDragTarget.

Usage:

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

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

The default implementations of getExpansionState and setExpansionState uses the toggledNodes Set to manage the expansion state of tree nodes.

Those methods can be overridden to use other data structures as 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, ParentProvider<T>? parentProvider, bool defaultExpansionState = false})
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
defaultExpansionState bool
Determines the initial expansion state of tree nodes.
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
parentProvider ParentProvider<T>
A getter callback that should return the direct parent of the tree node that is given to it or null if given a root node.
latefinal
roots Iterable<T>
The roots of the tree.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
toggledNodes Set<T>
Holds all the expanded OR collapsed nodes, depending on the value of defaultExpansionState.
latefinal

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
breadthFirstSearch({Iterable<T>? startingNodes, ValuePredicate<T>? descendCondition, ValuePredicate<T>? returnCondition, Visitor<T>? onTraverse}) → T?
Traverses the subtrees of startingNodes in breadth first order. If startingNodes is not provided, roots will be used instead.
checkNodeHasAncestor({required T node, required T potentialAncestor, bool checkForEquality = false}) bool
Checks if potentialAncestor is present in the path from node to its root node.
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, ValuePredicate<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
Traverses the tree looking for nodes that match the given predicate.
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