Skip to content

Commit

Permalink
fix embraceitmobile#33: on inserting a child, the child node spawned …
Browse files Browse the repository at this point in the history
…at the wrong index when the parent is expanded and has children
  • Loading branch information
jawwad-hassan89 committed Aug 18, 2024
1 parent b821aaf commit 3ec7314
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions lib/tree_view/tree_view_state_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class TreeViewStateHelper<Data> {
element.level > parentNode.level);
if (insertAtIndex < 0) {
animatedListStateController.insertAll(
animatedListStateController.list.length - 1,
animatedListStateController.length - 1,
List.from(event.items),
);
} else {
Expand All @@ -106,10 +106,24 @@ class TreeViewStateHelper<Data> {
if (!(node.root as ITreeNode<Data>).isExpanded) {
expansionBehaviourController.expandNode(node.root as ITreeNode<Data>);
} else {
final parentNode = node.root;
int actualIndex = event.index;

for (int i = event.index + 1;
i < animatedListStateController.length;
i++) {
if (animatedListStateController.list[i].level >
parentNode.level + 1) {
actualIndex++;
} else {
break;
}
}

animatedListStateController.insertAll(
animatedListStateController.showRootNode
? event.index + 1
: event.index,
? actualIndex + 1
: actualIndex,
List.from(event.items));
}
if (focusToNewNode) {
Expand All @@ -125,8 +139,22 @@ class TreeViewStateHelper<Data> {
if (!parentNode.isExpanded) {
expansionBehaviourController.expandNode(parentNode);
} else {
int actualIndex = parentIndex + event.index;

for (int i = parentIndex + event.index + 1;
i < animatedListStateController.length;
i++) {
if (animatedListStateController.list[i].level >
parentNode.level + 1) {
actualIndex++;
} else {
break;
}
}
animatedListStateController.insertAll(
parentIndex + 1 + event.index, List.from(event.items));
actualIndex + 1,
List.from(event.items),
);
}
if (focusToNewNode) {
expansionBehaviourController.scrollToLastVisibleChild(parentNode);
Expand Down

0 comments on commit 3ec7314

Please sign in to comment.