Fix: Flicker when reorderable list doesn't change its position (#151026)

Fix: Flicker when no update in index of dragged item

Resolves #150843

This PR ensures that even if we move dragged item anywhere in list and comeback to initial position, we smoothly adjust to that position.
This commit is contained in:
Kishan Rathore
2024-09-13 22:28:00 +05:30
committed by GitHub
parent 2812d4685c
commit bbc17fae3a
2 changed files with 70 additions and 2 deletions

View File

@@ -871,7 +871,7 @@ class SliverReorderableListState extends State<SliverReorderableList> with Ticke
// Find the new index for inserting the item being dragged.
int newIndex = _insertIndex!;
for (final _ReorderableItemState item in _items.values) {
if (item.index == _dragIndex! || !item.mounted) {
if ((_reverse && item.index == _dragIndex!) || !item.mounted) {
continue;
}
@@ -902,7 +902,13 @@ class SliverReorderableListState extends State<SliverReorderableList> with Ticke
newIndex = item.index;
}
} else {
if (itemStart <= proxyItemStart && proxyItemStart <= itemMiddle) {
if (item.index == _dragIndex!) {
// If end of the proxy is not in ending half of item,
// we don't process, because it's original dragged item.
if (itemMiddle <= proxyItemEnd && proxyItemEnd <= itemEnd) {
newIndex = _dragIndex!;
}
} else if (itemStart <= proxyItemStart && proxyItemStart <= itemMiddle) {
// The start of the proxy is in the beginning half of the item, so
// we should swap the item with the gap and we are done looking for
// the new index.

View File

@@ -717,6 +717,68 @@ void main() {
expect(tester.getTopLeft(find.text('item 0')), const Offset(0, 500));
});
testWidgets('SliverReorderableList - properly animates the drop at starting position', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/150843
final List<int> items = List<int>.generate(3, (int index) => index);
// The TestList is 300x100 SliverReorderableList with 3 items 100x100 each.
// Each item has a text widget with 'item $index' that can be moved by a
// press and drag gesture. For this test the first item is at the top
await tester.pumpWidget(TestList(items: items));
expect(tester.getTopLeft(find.text('item 0')), Offset.zero);
expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 100));
// Drag item 0 downwards and then upwards.
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('item 0')));
await tester.pump(kPressTimeout);
await drag.moveBy(const Offset(0, 100));
await tester.pumpAndSettle();
await drag.moveBy(const Offset(0, -110));
await tester.pump();
expect(tester.getTopLeft(find.text('item 0')), const Offset(0, -10));
expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 100));
// Now leave the drag, it should go to index 0.
await drag.up();
await tester.pump();
// It should not go to index 1 and come back
await tester.pump(const Duration(milliseconds: 200));
expect(tester.getTopLeft(find.text('item 0')).dy, lessThan(50));
});
testWidgets('SliverReorderableList - properly animates the drop at starting position with reverse:true', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/150843
final List<int> items = List<int>.generate(3, (int index) => index);
// The TestList is 300x100 SliverReorderableList with 3 items 100x100 each.
// Each item has a text widget with 'item $index' that can be moved by a
// press and drag gesture. For this test the first item is at the top
await tester.pumpWidget(TestList(items: items,reverse: true));
expect(tester.getTopLeft(find.text('item 2')), const Offset(0, 300.0));
expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 400.0));
// Drag item 2 downwards and then upwards.
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('item 2')));
await tester.pump(kPressTimeout);
await drag.moveBy(const Offset(0, 100));
await tester.pumpAndSettle();
await drag.moveBy(const Offset(0, -110));
await tester.pump();
expect(tester.getTopLeft(find.text('item 2')), const Offset(0, 290));
expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 300));
// Now leave the drag, it should go to index 1.
await drag.up();
await tester.pump();
// It should not go to index 0 and come back
await tester.pump(const Duration(milliseconds: 200));
expect(tester.getTopLeft(find.text('item 2')).dy, greaterThan(350));
});
testWidgets('SliverReorderableList calls onReorderStart and onReorderEnd correctly', (WidgetTester tester) async {
final List<int> items = List<int>.generate(8, (int index) => index);
int? startIndex, endIndex;