When I want to expand a certain node, UI error occurred
Try introducing a delay before expanding the intended node. May be it'll help to resolve the UI error you are facing.
I have tried delaying, but it seems to have no effect. Currently, I am starting from the first level using while (node. parent) to avoid this situation
I can confirm this issue:
"1. Obergeschoß" should be below "Stiege 2". The tree is correct if i am not calling expandNode multiple times. I would like to expand a certain node path to show a folder that a user has preselected. Adding a delay does not help.
Here is the code i'm using
onTreeReady: (controller) {
setState(() {
treeViewController = controller;
});
if (widget.currentOrderUnitId != null && treeResult.preselectedNode != null) {
_expandNodes(
controller: controller,
preselectedNode: treeResult.preselectedNode!,
directionRootToNode: true,
);
}
},
...
void _expandNodes(
{required TreeViewController<OrderUnitWrapper, TreeNode<OrderUnitWrapper>> controller,
required TreeNode<OrderUnitWrapper> preselectedNode,
required bool directionRootToNode}) {
final String path = preselectedNode.path;
// example path: 623c79a5-3ae9-4706-bad0-7dd8093f3d1b.01JQPXYM0EMS9CMMRHM84Y9RZK.01JQPXYM0EJE7RHGMYV2V92CR3
// Split the path by the dot separator to get individual node IDs
final List<String> nodeIds = path.split(".");
if (nodeIds.length >= 2) {
// Generate all the paths from shortest (root) to longest (preselected node's parent)
List<String> pathsToExpand = [];
// Build all possible paths from root to the target node (excluding the target itself)
for (int i = 1; i < nodeIds.length; i++) {
// Creating paths of increasing length, starting from the root
String currentPath = nodeIds[0];
for (int j = 1; j < i; j++) {
currentPath = "$currentPath.${nodeIds[j]}";
}
pathsToExpand.add(currentPath);
}
// If we're expanding from root to node, use the paths as they are (top-down)
// If expanding from node to root, reverse the order (bottom-up)
if (!directionRootToNode) {
// Reverse the list to go from bottom up (nearest ancestor first)
pathsToExpand = pathsToExpand.reversed.toList();
}
// Now expand the nodes in the appropriate order based on directionRootToNode
for (final String nodePath in pathsToExpand) {
final currentNode = controller.elementAt(nodePath);
controller.expandNode(currentNode);
}
}
}
expanding from root to node works - expanding from node to root shows the UI error described above