|
|
|
@ -48,31 +48,38 @@ class _OutlineExplorerState extends State<OutlineExplorer> {
@@ -48,31 +48,38 @@ class _OutlineExplorerState extends State<OutlineExplorer> {
|
|
|
|
|
|
|
|
|
|
// 过滤节点方法 - 递归版本 |
|
|
|
|
List<OutlineNode> _filterNodes(List<OutlineNode> nodes) { |
|
|
|
|
if (_searchQuery.isEmpty) { |
|
|
|
|
return nodes; |
|
|
|
|
void resetVisibility(List<OutlineNode> nodes) { |
|
|
|
|
for (final node in nodes) { |
|
|
|
|
node.isVisible = true; |
|
|
|
|
if (node.isDirectory) { |
|
|
|
|
resetVisibility(node.children); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 递归过滤函数 |
|
|
|
|
List<OutlineNode> recursiveFilter(List<OutlineNode> nodesToFilter, int currentDepth) { |
|
|
|
|
final List<OutlineNode> result = []; |
|
|
|
|
|
|
|
|
|
for (final node in nodesToFilter) { |
|
|
|
|
// 如果是第一层子节点(假设根节点深度为0,第一层子节点深度为1) |
|
|
|
|
if (node.title.toLowerCase().contains(_searchQuery.toLowerCase())) { |
|
|
|
|
result.add(node); |
|
|
|
|
void applySearchFilter(List<OutlineNode> nodes, String query) { |
|
|
|
|
for (final node in nodes) { |
|
|
|
|
// 递归处理子节点 |
|
|
|
|
if (node.isDirectory && node.depth < 2) { |
|
|
|
|
applySearchFilter(node.children, query); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 递归处理子节点,增加深度 |
|
|
|
|
if (node.isDirectory && node.children.isNotEmpty) { |
|
|
|
|
result.addAll(recursiveFilter(node.children, currentDepth + 1)); |
|
|
|
|
// 设置可见性:自身匹配或子节点可见 |
|
|
|
|
final bool isMatch = node.title.toLowerCase().contains(query.toLowerCase()); |
|
|
|
|
final bool hasVisibleChild = |
|
|
|
|
node.depth < 2 && node.isDirectory && node.children.any((child) => child.isVisible); |
|
|
|
|
|
|
|
|
|
node.isVisible = isMatch || hasVisibleChild; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
if (_searchQuery.isEmpty) { |
|
|
|
|
resetVisibility(nodes); |
|
|
|
|
return nodes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 从深度0开始递归 |
|
|
|
|
return recursiveFilter(nodes, 0); |
|
|
|
|
applySearchFilter(nodes, _searchQuery); |
|
|
|
|
return nodes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
@ -94,7 +101,7 @@ class _OutlineExplorerState extends State<OutlineExplorer> {
@@ -94,7 +101,7 @@ class _OutlineExplorerState extends State<OutlineExplorer> {
|
|
|
|
|
controller: _searchController, |
|
|
|
|
style: const TextStyle(fontSize: 12), // 输入文字大小为10 |
|
|
|
|
decoration: InputDecoration( |
|
|
|
|
hintText: '搜索节点...', |
|
|
|
|
hintText: '搜索Tag...', |
|
|
|
|
hintStyle: const TextStyle(fontSize: 12), // 提示文字大小为10 |
|
|
|
|
prefixIcon: const Icon( |
|
|
|
|
Icons.search, |
|
|
|
|