Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tree_controller.dart #42

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/src/tree_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,54 @@ class TreeController<T extends Object> with ChangeNotifier {
rebuild();
}

/// Expands all nodes of this tree consequently.
void expandAll() => expandCascading(roots);

/// Collapses all nodes of this tree consequently.
void collapseAll() => collapseCascading(roots);

/// Whether all root nodes of this tree are expanded.
///
/// To know this it is required to know if root nodes contained in
/// the cache of expanded nodes.
bool get areAllRootsExpanded => roots.every(getExpansionState);

/// Whether all root nodes of this tree are collapsed.
///
/// To know this it is required to know if root nodes does not contained in
/// the cache of expanded nodes.
bool get areAllRootsCollapsed => !roots.any(getExpansionState);

/// Whether all nodes of this tree are expanded.
bool get isTreeExpanded {
final totalNodes = [], expandedNodes = [];
depthFirstTraversal(
onTraverse: (entry) {
totalNodes.add(entry);
if (entry.isExpanded) {
expandedNodes.add(entry);
}
},
descendCondition: (node) => true,
);
return totalNodes.length == expandedNodes.length;
}

/// Whether all nodes of this tree are collapsed.
bool get isTreeCollapsed {
final totalNodes = [], collapsedNodes = [];
depthFirstTraversal(
onTraverse: (entry) {
totalNodes.add(entry);
if (!entry.isExpanded) {
collapsedNodes.add(entry);
}
},
descendCondition: (node) => true,
);
return totalNodes.length == collapsedNodes.length;
}

void _applyCascadingAction(Iterable<T> nodes, Visitor<T> action) {
for (final T node in nodes) {
action(node);
Expand Down