|
|
|
@ -9,18 +9,33 @@ class FileService {
@@ -9,18 +9,33 @@ class FileService {
|
|
|
|
|
|
|
|
|
|
if (await directory.exists()) { |
|
|
|
|
final entities = directory.listSync(); |
|
|
|
|
|
|
|
|
|
// 遍历目录实体并创建FileNode |
|
|
|
|
for (final entity in entities) { |
|
|
|
|
nodes.add( |
|
|
|
|
FileNode( |
|
|
|
|
name: entity.path.split(Platform.pathSeparator).last, |
|
|
|
|
path: entity.path, |
|
|
|
|
isDirectory: entity is Directory, |
|
|
|
|
depth: parentDepth + 1, // 关键修复:基于父节点深度+1 |
|
|
|
|
depth: parentDepth + 1, |
|
|
|
|
children: [], |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 排序:文件夹在前按字母排序,文件在后按字母排序 |
|
|
|
|
nodes.sort((a, b) { |
|
|
|
|
if (a.isDirectory && !b.isDirectory) { |
|
|
|
|
return -1; // a是文件夹,b是文件,a排在前面 |
|
|
|
|
} else if (!a.isDirectory && b.isDirectory) { |
|
|
|
|
return 1; // a是文件,b是文件夹,b排在前面 |
|
|
|
|
} else { |
|
|
|
|
// 都是文件夹或都是文件时,按名称字母排序 |
|
|
|
|
return a.name.compareTo(b.name); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nodes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|