toggleRowExpanded method

void toggleRowExpanded(
  1. String rowId
)

Toggle tree row expanded/collapsed

Implementation

void toggleRowExpanded(String rowId) {
  final newExpandedIds = Set<String>.from(_state.expandedRowIds);

  if (newExpandedIds.contains(rowId)) {
    newExpandedIds.remove(rowId);
  } else {
    newExpandedIds.add(rowId);
  }

  // Recalculate visible tree rows (include total rows at top)
  final flattenedRegularRows = _treeManager.flattenTree(
    rows: _state.regularRows,
    expandedIds: newExpandedIds,
  );

  final newTreeRows = [
    // Prepend total rows as leaf nodes
    ..._state.totalRows.asMap().entries.map((entry) {
      return TreeRow(
        data: entry.value,
        depth: 0,
        hasChildren: false,
        id: '_total_${entry.key}',
      );
    }),
    ...flattenedRegularRows,
  ];

  // Check if all expandable rows are now expanded
  final allExpandableIds = _treeManager.getAllExpandableIds(_state.regularRows);
  final allExpanded = allExpandableIds.isNotEmpty &&
      allExpandableIds.every((id) => newExpandedIds.contains(id));

  _state = _state.copyWith(
    expandedRowIds: newExpandedIds,
    treeRows: newTreeRows,
    allExpanded: allExpanded,
    loadedRowCount: newTreeRows.length, // Load all visible tree rows
  );
  notifyListeners();
}