animated_tree_view icon indicating copy to clipboard operation
animated_tree_view copied to clipboard

Order issue when add a new node

Open AlehSabadashGmail opened this issue 2 years ago • 4 comments

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

AlehSabadashGmail avatar Nov 13 '23 09:11 AlehSabadashGmail

I have a similiar behaivor.

Starting data

  • Folder 1
    • Folder A
      • Child 1

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

  • Folder 1
    • Folder A
    • Folder B
      • Child 1

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

  • Folder 1
    • Folder A
      • Child 1 (child is on correct position, if i expand Folder A)
    • Folder B

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

  • Folder 1
    • Folder A
      • Child 1
    • Folder B

mjauernig avatar Nov 20 '23 12:11 mjauernig

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

HoKim98 avatar Dec 26 '23 16:12 HoKim98

Also has the same problem

shidenggui avatar Jun 22 '24 14:06 shidenggui

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

olerhan avatar Jul 02 '24 00:07 olerhan