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

Expand all nodes default or special node #5

Closed
daonvshu opened this issue Oct 21, 2022 · 3 comments
Closed

Expand all nodes default or special node #5

daonvshu opened this issue Oct 21, 2022 · 3 comments
Assignees

Comments

@daonvshu
Copy link

Hi, Is there any way to expand all child nodes or specified node after all view build is complete? I changed the 'isExpanded' property of the TreeNode to true but it had no effect.

@jawwad-hassan89 jawwad-hassan89 self-assigned this Nov 7, 2022
@jawwad-hassan89
Copy link
Collaborator

To programmatically expand or collapse a node, use the toggleExpansion method of the TreeViewController

To get the TreeViewController, wrap the TreeViewState inside a GlobalKey like final globalKey = GlobalKey<TreeViewState>(), and then get the controller from the currentState like this globalKey.currentState?.controller;

Full code sample to use the TreeViewController:

class _MyHomePageState extends State<MyHomePage> {
  final globalKey = GlobalKey<TreeViewState>();

  TreeViewController? get controller => globalKey.currentState?.controller;
  final tree = TreeNode<String>();

  void toggleExpansion(String nodePath) {
    final node = controller?.elementAt(nodePath);
    if (node != null) controller?.toggleExpansion(node);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: TreeView.simple<String>(
        key: globalKey,
        tree: tree,
        builder: (context, level, item) => ListTile(
          title: Text("Item ${item.level}-${item.key}"),
        ),
      ),
    );
  }
}

@kespaldon
Copy link

hi, you didn't really answer the first question

expand all child nodes

if you could respond to this that'd be great

@jawwad-hassan89
Copy link
Collaborator

@kespaldon right now there is no builtin method to expand all the children of a node. So you will have to recursively iterate through the children, and expand them. I will add a utility method to handle expansion of all children in the next release.
For the time being you can use this handy extension to achieve the same.

extension TreeControllerX<Data, Tree extends ITreeNode<Data>>
    on TreeViewController {
  /// Utility method for programmatically expanding all the child nodes. By default
  /// only the immediate children of the node will be expanded.
  /// Set [recursive] to true for expanding all the child nodes until the leaf is
  /// reached.
  void expandAllChildren(Tree node, {bool recursive = false}) {
    for (final child in node.childrenAsList) {
      toggleExpansion(child as Tree);
      if (child.childrenAsList.isNotEmpty) expandAllChildren(child);
    }
  }
}

jawwad-hassan89 added a commit to jawwad-hassan89/animated_tree_view that referenced this issue Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants