GridSheetSnapshot class
Immutable snapshot of grid state for undo/redo operations.
This class captures a complete snapshot of the grid's data and configuration at a specific point in time. It's used by undo/redo systems to save and restore grid state.
Captured State
The snapshot includes:
- Data: All rows and columns with their current state
- Sorting: Active sort keys and directions
- Filtering: Applied filter criteria
- Formatting: Conditional formatting rules
- Pagination: Current page and page size
Usage Example
class GridUndoRedoManager {
final List<GridSheetSnapshot> _undoStack = [];
final List<GridSheetSnapshot> _redoStack = [];
// Save current state before making changes
void saveSnapshot(GridSheetManager manager) {
final snapshot = GridSheetSnapshot(
rows: List.from(manager.rows),
columns: List.from(manager.columns),
sortKeys: List.from(manager.sortKeys),
filters: Map.from(manager.filters),
conditionalFormatRules: List.from(manager.conditionalFormatRules),
currentPage: manager.currentPage,
rowsPerPage: manager.rowsPerPage,
);
_undoStack.add(snapshot);
_redoStack.clear(); // Clear redo stack on new change
}
// Restore previous state
void undo(GridSheetManager manager) {
if (_undoStack.isEmpty) return;
// Save current state to redo stack
saveToRedoStack(manager);
// Restore previous state
final snapshot = _undoStack.removeLast();
restoreSnapshot(manager, snapshot);
}
void redo(GridSheetManager manager) {
if (_redoStack.isEmpty) return;
// Save current state to undo stack
saveSnapshot(manager);
// Restore next state
final snapshot = _redoStack.removeLast();
restoreSnapshot(manager, snapshot);
}
void restoreSnapshot(GridSheetManager manager, GridSheetSnapshot snapshot) {
manager.setRows(snapshot.rows);
manager.setColumns(snapshot.columns);
manager.setSortKeys(snapshot.sortKeys);
manager.addFilters(snapshot.filters);
manager.setConditionalFormatRules(snapshot.conditionalFormatRules);
manager.goToPage(snapshot.currentPage);
manager.changeRowsPerPage(snapshot.rowsPerPage);
}
}
Deep Copy Considerations
The snapshot constructor performs shallow copies of collections. If you need true immutability with deep copies, consider:
GridSheetSnapshot createDeepCopy(GridSheetManager manager) {
return GridSheetSnapshot(
rows: manager.rows.map((r) => GridSheetRow(
key: r.key,
index: r.index,
data: List.from(r.data), // Deep copy data
isSelected: r.isSelected,
height: r.height,
state: r.state,
)).toList(),
// ... similar deep copies for other fields
columns: manager.columns.map((c) => c.copyWith()).toList(),
sortKeys: manager.sortKeys.map((s) => s.copyWith()).toList(),
filters: Map.from(manager.filters),
conditionalFormatRules: manager.conditionalFormatRules
.map((r) => r.copyWith()).toList(),
currentPage: manager.currentPage,
rowsPerPage: manager.rowsPerPage,
);
}
Memory Considerations
Snapshots can consume significant memory for large grids. Consider:
- Limiting undo/redo stack size
- Implementing compression for old snapshots
- Using delta-based undo (store changes, not full state)
- Clearing history periodically or on save
See also:
- GridSheetManager, which provides the state to snapshot
- GridSheetRow, GridSheetColumn, which are captured in snapshots
Constructors
-
GridSheetSnapshot({required List<
GridSheetRow> rows, required List<GridSheetColumn> columns, required List<GridSheetSortState> sortKeys, required Map<String, List> filters, required List<GridSheetConditionalFormatRule> conditionalFormatRules, required int currentPage, required int rowsPerPage}) -
Creates an immutable snapshot of grid state.
const
Properties
-
columns
→ List<
GridSheetColumn> -
All columns in the grid at snapshot time.
final
-
conditionalFormatRules
→ List<
GridSheetConditionalFormatRule> -
Conditional formatting rules at snapshot time.
final
- currentPage → int
-
The current page number at snapshot time (1-based).
final
-
filters
→ Map<
String, List> -
Active filter criteria at snapshot time.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
rows
→ List<
GridSheetRow> -
All rows in the grid at snapshot time.
final
- rowsPerPage → int
-
The rows per page setting at snapshot time.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
sortKeys
→ List<
GridSheetSortState> -
Active sort keys at snapshot time.
final
Methods
-
copyWith(
{List< GridSheetRow> ? rows, List<GridSheetColumn> ? columns, List<GridSheetSortState> ? sortKeys, Map<String, List> ? filters, List<GridSheetConditionalFormatRule> ? conditionalFormatRules, int? currentPage, int? rowsPerPage}) → GridSheetSnapshot - Creates a copy of this configuration with the given fields replaced.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited