diff --git a/packages/flutter/lib/src/widgets/reorderable_list.dart b/packages/flutter/lib/src/widgets/reorderable_list.dart index c838f137dc..689013119a 100644 --- a/packages/flutter/lib/src/widgets/reorderable_list.dart +++ b/packages/flutter/lib/src/widgets/reorderable_list.dart @@ -673,7 +673,7 @@ class SliverReorderableListState extends State with Ticke setState(() { if (_insertIndex! < widget.itemCount - 1) { // Find the location of the item we want to insert before - _finalDropPosition = _itemOffsetAt(_insertIndex!); + _finalDropPosition = _itemOffsetAt(_insertIndex! + (_reverse ? 1 : 0)); } else { // Inserting into the last spot on the list. If it's the only spot, put // it back where it was. Otherwise, grab the second to last and move diff --git a/packages/flutter/test/widgets/reorderable_list_test.dart b/packages/flutter/test/widgets/reorderable_list_test.dart index f99edf024f..638a47a31c 100644 --- a/packages/flutter/test/widgets/reorderable_list_test.dart +++ b/packages/flutter/test/widgets/reorderable_list_test.dart @@ -268,6 +268,49 @@ void main() { expect(getItemFadeTransition(), findsNothing); }); + testWidgets('SliverReorderableList - properly animates the drop at starting position in a reversed list', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/84625 + final List items = List.generate(8, (int index) => index); + + Future pressDragRelease(Offset start, Offset delta) async { + final TestGesture drag = await tester.startGesture(start); + await tester.pump(kPressTimeout); + await drag.moveBy(delta); + await tester.pumpAndSettle(); + await drag.up(); + await tester.pump(); + } + + // The TestList is 800x600 SliverReorderableList with 8 items 800x100 each. + // Each item has a text widget with 'item $index' that can be moved by a + // press and drag gesture. For this test we are reversing the order so + // the first item is at the bottom. + await tester.pumpWidget(TestList(items: items, reverse: true)); + + expect(tester.getTopLeft(find.text('item 0')), const Offset(0, 500)); + expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 400)); + + // Drag item 0 downwards off the edge and let it snap back. It should + // smoothly animate back up. + await pressDragRelease(tester.getCenter(find.text('item 0')), const Offset(0, 50)); + expect(tester.getTopLeft(find.text('item 0')), const Offset(0, 550)); + expect(tester.getTopLeft(find.text('item 1')), const Offset(0, 400)); + + // After the first several frames we should be moving closer to the final position, + // not further away as was the case with the original bug. + await tester.pump(const Duration(milliseconds: 10)); + expect(tester.getTopLeft(find.text('item 0')).dy, lessThan(550)); + + // Sample the middle (don't use exact values as it depends on the internal + // curve being used). + await tester.pump(const Duration(milliseconds: 125)); + expect(tester.getTopLeft(find.text('item 0')).dy, lessThan(550)); + + // Wait for it to finish, it should be back to the original position + await tester.pumpAndSettle(); + expect(tester.getTopLeft(find.text('item 0')), const Offset(0, 500)); + }); + testWidgets('ReorderableList asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async { final List numbers = [0,1,2]; expect(() => ReorderableList( @@ -594,12 +637,14 @@ class TestList extends StatefulWidget { this.iconColor, this.proxyDecorator, required this.items, + this.reverse = false, }) : super(key: key); final List items; final Color? textColor; final Color? iconColor; final ReorderItemProxyDecorator? proxyDecorator; + final bool reverse; @override State createState() => _TestListState(); @@ -618,6 +663,7 @@ class _TestListState extends State { builder: (BuildContext outerContext, StateSetter setState) { final List items = widget.items; return CustomScrollView( + reverse: widget.reverse, slivers: [ SliverReorderableList( itemBuilder: (BuildContext context, int index) {