embraceitmobile / animated_tree_view

Animated TreeView based on AnimatedList allows building fully customizable Nodes that can be nested to infinite levels and children.
https://pub.dev/packages/animated_tree_view
MIT License
59 stars 29 forks source link

Order issue when add a new node #33

Closed AlehSabadashGmail closed 3 weeks ago

AlehSabadashGmail commented 10 months ago

Package version - 2.1.0 Type of tree - TreeView.simpleTyped Flutter version - 3.13.6

There is an order issue when I add a node to a parent node where there are already expanded nodes, a new nodes is added to the wrong place

https://github.com/embraceitmobile/animated_tree_view/assets/90776958/f6d07408-64ba-44d7-90ab-2e5d03a417c2

mjauernig commented 10 months ago

I have a similiar behaivor.

Starting data

When i update the tree (add Folder B to Folder 1) and Folder A is expanded the visible result is:

When i update the tree (add Folder B to Folder 1) and Folder A is collapsed the result is ok:

If the starting data has already an existing second folder and i add a third folder the result is also ok.

HoKim98 commented 9 months ago

Using overridden root() factory for the root node may resolve this issue.


class MyTreeNode extends TreeNode<MyTreeData> {
  factory MyTreeNode.root({MyTreeData? data}) => MyTreeNode._your_own_factory(
        key: INode.ROOT_KEY,
        data: data,
        // TODO(user): stuffs
      )
        ..isLastChild = true
        ..cacheChildIndices();
}
shidenggui commented 3 months ago

Also has the same problem

olerhan commented 3 months ago
...
  Widget buildInsertBelowButton(IndexedTreeNode item) {
    return Padding(
      padding: const EdgeInsets.only(right: 16.0),
      child: TextButton(
        style: TextButton.styleFrom(
          foregroundColor: Colors.green[800],
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(4)),
          ),
        ),
        child: Text("Insert Below", style: TextStyle(color: Colors.green)),
        onPressed: () {
          _controller?.collapseNode(item); // My solution.
          item.parent?.insertAfter(item, IndexedTreeNode());
        },
      ),
    );
  }
  ...

In my case, my solution is to just add collapseNode for the current node in the insertBelowButton widget's onPressed method. This issue needs to be fixed at the API level. @jawwad-hassan89 @wasimshigri Thank you.