Comments (3)
Thanks, I have tried your code, copying node children solved the problem!
addChild
will first remove node from tree and add it then, that's why the original rootNode was modified.
from flutter_tree_view.
I currently use following code to do the trick.
However, I found that rootNode children is modified during buildSearchData
; Therefore, I had to request api and regenerate all tree when searchContent is empty.
void onSearch() {
if (searchContent.trim().isEmpty) {
getData();
} else {
final TreeNode newRootNode = TreeNode(id: 'SearchRoot');
final Set<TreeNode> searchData =
buildSearchData(searchContent, Set<TreeNode>.from(rootNode.children));
newRootNode.addChildren(searchData);
treeController = TreeViewController(rootNode: newRootNode);
}
treeController.expandAll();
setState(() {});
}
Set<TreeNode> buildSearchData(String content, Iterable<TreeNode> nodes) {
return nodes
.map(
(TreeNode e) {
final TreeNode treeNode =
TreeNode(id: e.id, label: e.label, data: e.data);
if (e.hasChildren) {
if (e.label.contains(content)) {
final Set<TreeNode> children = Set<TreeNode>.from(e.children);
treeNode.addChildren(children);
return treeNode;
} else {
final Iterable<TreeNode> searchData =
buildSearchData(content, e.children);
if (searchData.isNotEmpty) {
searchData.forEach(treeNode.addChild);
return treeNode;
}
}
} else if (e.label.contains(content)) {
return treeNode;
} else if (e.data is Map<String, dynamic>) {
final Map<String, dynamic> data = e.data as Map<String, dynamic>;
// tree data satisfies search content
if (data.validateAny(
(Map<String, dynamic> data) => <bool?>[
cast<String?>(data['standards'])?.contains(content),
cast<String?>(data['unit'])?.contains(content),
],
)) {
return treeNode;
}
}
},
)
.whereNotNull()
.toSet();
}
from flutter_tree_view.
Hey @lvyandev! Thank you for opening this issue.
Could you please add a "expected/actual" result of your functions?
Make sure all your ids are unique, the TreeNode
objects get their identity from its id, label and data.
I see you are copying the nodes into a new tree, but you are not copying in the following part:
if (e.hasChildren) {
if (e.label.contains(content)) {
// You should probably copy each child into a new TreeNode here, right?
final Set<TreeNode> children = Set<TreeNode>.from(e.children);
// Before adding them to the treeNode children as below
treeNode.addChildren(children);
return treeNode;
} else {
// ...
}
}
The TreeNode.addChild
removes the child from its old parent before adding it to the new one, so if you don't copy them over to the new tree (i.e. create new objects), when showing the old tree again, those nodes will be missing because they are now part of another tree (i.e. were detached from old root and attached to the new one).
Try the following:
if (e.hasChildren) {
if (e.label.contains(content)) {
for (final TreeNode child in e.children) {
final TreeNode newChild = TreeNode(id: child.id, label: child.label, data: child.data);
treeNode.addChild(newChild);
}
return treeNode;
} else {
// ...
}
}
I haven't tried the code above, let me know if it works.
from flutter_tree_view.
Related Issues (20)
- Feature request: Support for drag handlers HOT 4
- Problem with first root HOT 9
- I encountered a problem HOT 1
- Color of selected node HOT 1
- Incomplete expansion state when expanding too many nodes in `AnimatedTreeView` HOT 4
- Way to restore the state of a `TreeView`? HOT 1
- Feature Request: Add onWillAcceptWithDetail HOT 2
- Thanks
- How in TreeView if I expand one folder and collapse other folder HOT 2
- Duplicated key in scaffold HOT 1
- Show togglable expansion icon on leaf node HOT 1
- Drag and Drop Multiple Nodes HOT 1
- Drag only on selected target HOT 2
- flutter_fancy_tree_view with AlertDialog HOT 3
- Show root node in tree HOT 2
- Multiple Widgets used the same GlobalKey HOT 5
- support for the same node in mutiple places in the tree, please, like directed graph traversal.. HOT 3
- I am finding it difficult to restore expanded nodes on widget rebuild HOT 5
- MultiColumn TreeView HOT 1
- Callback when TreeNode build has completed? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_tree_view.