Coder Social home page Coder Social logo

Comments (3)

jawwad-hassan89 avatar jawwad-hassan89 commented on September 26, 2024

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}"),
        ),
      ),
    );
  }
}

from animated_tree_view.

kespaldon avatar kespaldon commented on September 26, 2024

hi, you didn't really answer the first question

expand all child nodes

if you could respond to this that'd be great

from animated_tree_view.

jawwad-hassan89 avatar jawwad-hassan89 commented on September 26, 2024

@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);
    }
  }
}

from animated_tree_view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.