diff --git a/packages/flutter/lib/src/widgets/reorderable_list.dart b/packages/flutter/lib/src/widgets/reorderable_list.dart index 9acef85301..fa2759a3e0 100644 --- a/packages/flutter/lib/src/widgets/reorderable_list.dart +++ b/packages/flutter/lib/src/widgets/reorderable_list.dart @@ -871,7 +871,7 @@ class SliverReorderableListState extends State 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 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. diff --git a/packages/flutter/test/widgets/reorderable_list_test.dart b/packages/flutter/test/widgets/reorderable_list_test.dart index cd1c25c529..43c25a1c54 100644 --- a/packages/flutter/test/widgets/reorderable_list_test.dart +++ b/packages/flutter/test/widgets/reorderable_list_test.dart @@ -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 items = List.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 items = List.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 items = List.generate(8, (int index) => index); int? startIndex, endIndex;