flattenTree method
Flatten tree data into visible rows based on expanded state Only includes rows whose parents are expanded
Implementation
List<TreeRow> flattenTree({
required List<Map<String, dynamic>> rows,
required Set<String> expandedIds,
int depth = 0,
String parentId = '',
}) {
final result = <TreeRow>[];
for (var i = 0; i < rows.length; i++) {
final row = rows[i];
final rowId = _generateRowId(row, i, parentId);
final hasChildren = _hasChildren(row);
result.add(TreeRow(
data: row,
depth: depth,
hasChildren: hasChildren,
id: rowId,
));
// Recursively add children if this row is expanded
if (hasChildren && expandedIds.contains(rowId)) {
final children = (row['_children'] as List)
.whereType<Map<String, dynamic>>()
.toList();
result.addAll(flattenTree(
rows: children,
expandedIds: expandedIds,
depth: depth + 1,
parentId: rowId,
));
}
}
return result;
}