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

node.add() method causes break in linking tree lines: File explorer example #43

Closed Uche01 closed 3 weeks ago

Uche01 commented 6 months ago

I am adding nodes directly to a parent using method: node.add(). Items are added but the lines showing node link appears to break for the newly added node. See code and image below: node.add(SeasonNode()); node-break-screenshot

pdsliuzhen commented 4 months ago

I have also encountered this problem, may I ask if the problem has been solved

Michal-MK commented 4 months ago

I solved it by cloing the repo and adding

  void fixLastChild() {
    for (var i = 0; i < childrenAsList.length; i++) {
      if (childrenAsList[i] is TreeNode) {
        (childrenAsList[i] as TreeNode).isLastChild = i == childrenAsList.length - 1;
      }
    }
  }

to listenable_node.dart and altering all the add addAll remove etc.. to call it after the super calls.

Then in _IndentationPainter I added an extra parameter isLastChild and initialize it from the node in constructor. Taking it from the node itself did not work for me.

return CustomPaint(
      foregroundPainter: _IndentationPainter(
        indentation: indentation,
        node: node,
        isLastChild: node.isLastChild,
        minLevelToIndent: minLevelToIndent,
      ),
      child: content,
    );

And alter it's shouldRepaint

   @override
  bool shouldRepaint(_IndentationPainter oldDelegate) {
    return indentation != oldDelegate.indentation || node != oldDelegate.node || isLastChild != oldDelegate.isLastChild;
  }

Do not that it is O(n) for every operation so for large trees may be inadequate. It works for my specific usecase.

olerhan commented 3 months ago

I'm not sure if this error originated from me, but seeing this error here made me start thinking that it was caused by the package. My solution was different. I created the following special methods. It's easier than the method above and I think it will work for everyone facing this problem. It worked for me.

  Future<void> insertNodeBelow(IndexedTreeNode<dynamic> referenceNode,
      IndexedTreeNode<dynamic> newNode) async {
    if (referenceNode.isLastChild) {
      referenceNode.isLastChild = false;
      newNode.isLastChild = true;
    }
    referenceNode.parent?.insertAfter(referenceNode, newNode);
//my code
  }

and...

  Future<void> addNode(
      IndexedTreeNode<dynamic> parent, IndexedTreeNode<dynamic> newNode) async {
    for (final child in parent.children) {
      if (child is IndexedTreeNode<dynamic>) {
        child.isLastChild = false;
      }
    }
    newNode.isLastChild = true;
    parent.add(newNode);
//my code
  }

This is it.

I hope the package owner does something to fix these bugs.