Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions lib/features/workspace/result_grid_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -735,15 +735,42 @@ class _VirtualResultGridState extends material.State<VirtualResultGrid> {
_lastKnownStagedRowCount = currentRows.length;

setState(() {
if (_sortColumnIndex != null || rowCountChanged) {
_updateSortedRows();
}
if (rowCountChanged) {
_updateSortedRows();
_widthsNeedUpdate = true;
} else if (widget.rowIndicesMapping == null) {
_refreshSortedRowValues(currentRows);
} else if (_sortColumnIndex != null) {
_updateSortedRows();
}
});
}

/// Applies staged cell values to the displayed rows without re-sorting.
///
/// A cell edit does not add or remove rows, so the current visual order is
/// kept (the edited row does not jump while typing) and only the row list is
/// rebuilt in O(N). Sorting again happens on a sort-column change, a row
/// insert / delete, or when the user re-applies the sort.
void _refreshSortedRowValues(List<List<String>> rows) {
if (_sortColumnIndex == null || _sortOrder == null) {
_sortedRows = rows;
return;
}
final order = _sortedToModelIndices;
if (order.length != rows.length) {
_updateSortedRows();
return;
}
// Drop any in-flight isolate sort: it was computed from older values.
_sortVersion++;
_sortedRows = List<List<String>>.generate(
order.length,
(i) => rows[order[i]],
growable: false,
);
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
Expand Down Expand Up @@ -772,7 +799,17 @@ class _VirtualResultGridState extends material.State<VirtualResultGrid> {
_editingCell = null;
widget.onRowSelected?.call(null);
}
_updateSortedRows();
// With a staging buffer and no filter mapping the displayed rows come from
// the buffer, and a cell edit is applied by [_onStagingBufferChanged]. The
// parent handing over a fresh `rows` list for that edit must not re-sort.
final onlyRowsChanged = oldWidget.columns == widget.columns &&
oldWidget.stagingBuffer == widget.stagingBuffer &&
oldWidget.rowIndicesMapping == widget.rowIndicesMapping;
final rowsFollowBuffer =
widget.stagingBuffer != null && widget.rowIndicesMapping == null;
if (!(onlyRowsChanged && rowsFollowBuffer)) {
_updateSortedRows();
}
}
}

Expand Down
91 changes: 91 additions & 0 deletions test/features/workspace/result_grid_sorting_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,95 @@ void main() {
expect(stagingBuffer.getCellValue(0, 0), equals('Charlie'));
});
});

group('VirtualResultGrid keeps sorted order stable while editing (#875)', () {
Future<DataGridStagingBuffer> pumpSorted(
WidgetTester tester, {
required bool rebuildParent,
}) async {
final columns = ['name', 'age'];
final buffer = DataGridStagingBuffer(
columns: columns,
rows: [
['Charlie', '30'], // model 0
['Alice', '10'], // model 1
['Bob', '20'], // model 2
],
);
addTearDown(buffer.dispose);

await tester.pumpWidget(
_testShell(
child: material.SizedBox(
width: 800,
height: 600,
child: rebuildParent
// Like ResultsTab: a fresh `rows` list on every buffer change.
? material.ListenableBuilder(
listenable: buffer,
builder: (_, __) => VirtualResultGrid(
columns: columns,
rows: buffer.effectiveRows,
stagingBuffer: buffer,
),
)
: VirtualResultGrid(
columns: columns,
rows: buffer.effectiveRows,
stagingBuffer: buffer,
),
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('name'));
await tester.pumpAndSettle();
return buffer;
}

double y(WidgetTester tester, String text) =>
tester.getTopLeft(find.text(text)).dy;

for (final rebuildParent in [false, true]) {
testWidgets(
'editing a cell keeps the row in place and shows the new value '
'(parent rebuilds: $rebuildParent)', (tester) async {
final buffer = await pumpSorted(tester, rebuildParent: rebuildParent);
// Sorted ascending: Alice, Bob, Charlie.
expect(y(tester, 'Alice') < y(tester, 'Bob'), isTrue);

buffer.setCell(1, 0, 'Zed'); // Alice -> Zed (model row 1)
await tester.pumpAndSettle();

expect(find.text('Alice'), findsNothing);
// Zed stays first instead of jumping below Charlie.
expect(y(tester, 'Zed') < y(tester, 'Bob'), isTrue);
expect(y(tester, 'Bob') < y(tester, 'Charlie'), isTrue);
});

testWidgets(
'edits keep mapping visual rows to the right model row '
'(parent rebuilds: $rebuildParent)', (tester) async {
final buffer = await pumpSorted(tester, rebuildParent: rebuildParent);

buffer.setCell(1, 1, '99');
await tester.pumpAndSettle();
await _secondaryClick(tester, find.text('Alice'));
await tester.tap(find.text('Delete Row'));
await tester.pumpAndSettle();

expect(buffer.getRowStatus(1), StagedRowStatus.deleted);
expect(buffer.getRowStatus(0), StagedRowStatus.unchanged);
});
}

testWidgets('inserting a row still re-sorts', (tester) async {
final buffer = await pumpSorted(tester, rebuildParent: true);

buffer.addRow(['Aaron', '5']);
await tester.pumpAndSettle();

expect(y(tester, 'Aaron') < y(tester, 'Alice'), isTrue);
});
});
}
Loading