From b4953c37694e7c8ab0e4daa15c4ab6deb97b7fde Mon Sep 17 00:00:00 2001 From: Delwin Mathew <84124091+opxdelwin@users.noreply.github.com> Date: Thu, 14 Sep 2023 04:25:05 +0530 Subject: [PATCH] Fix null check crash by ReorderableList (#132153) Fix issue where if you drag the last element of `ReorderableList` and put it in the same index, a `Null check` error arises. This happens as index in `_items` is out of bounds (when `reverse: true`). Fix is to check if last element, dragged element and drop index is same, and return as nothing has changed. Find this video attached. https://github.com/flutter/flutter/assets/84124091/8043cac3-eb08-42e1-87e7-8095ecab09dc Fixes issue #132077 --- .../lib/src/widgets/reorderable_list.dart | 19 ++++++++ .../test/widgets/reorderable_list_test.dart | 45 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/packages/flutter/lib/src/widgets/reorderable_list.dart b/packages/flutter/lib/src/widgets/reorderable_list.dart index 0f0d5e78cc..af912e5ac2 100644 --- a/packages/flutter/lib/src/widgets/reorderable_list.dart +++ b/packages/flutter/lib/src/widgets/reorderable_list.dart @@ -798,6 +798,25 @@ class SliverReorderableListState extends State with Ticke } void _dragEnd(_DragInfo item) { + // No changes required if last child is being inserted into the last position. + if ((_insertIndex! + 1 == _items.length) && _reverse) { + final RenderBox lastItemRenderBox = _items[_items.length - 1]!.context.findRenderObject()! as RenderBox; + final Offset lastItemOffset = lastItemRenderBox.localToGlobal(Offset.zero); + + // When drag starts, the corresponding element is removed from + // the list, and moves inside of [ReorderableListState.CustomScrollView], + // which gives [CustomScrollView] a variable height. + // + // So when the element is moved, delta would change accordingly, + // and since it's the last element, + // we animate it back to it's position and add it back to the list. + final double delta = item.itemSize.height; + + setState(() { + _finalDropPosition = Offset(lastItemOffset.dx, lastItemOffset.dy - delta); + }); + return; + } setState(() { if (_insertIndex == item.index) { _finalDropPosition = _itemOffsetAt(_insertIndex! + (_reverse ? 1 : 0)); diff --git a/packages/flutter/test/widgets/reorderable_list_test.dart b/packages/flutter/test/widgets/reorderable_list_test.dart index 1437c59710..496d2242b1 100644 --- a/packages/flutter/test/widgets/reorderable_list_test.dart +++ b/packages/flutter/test/widgets/reorderable_list_test.dart @@ -1309,6 +1309,51 @@ void main() { expect(offsetForFastScroller / offsetForSlowScroller, fastVelocityScalar / slowVelocityScalar); }); + + testWidgets('Null check error when dragging and dropping last element into last index with reverse:true', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/132077 + const int itemCount = 5; + final List items = List.generate(itemCount, (int index) => 'Item ${index+1}'); + + await tester.pumpWidget( + MaterialApp( + home: ReorderableList( + onReorder: (int oldIndex, int newIndex) { + if (newIndex > oldIndex) { + newIndex -= 1; + } + final String item = items.removeAt(oldIndex); + items.insert(newIndex, item); + }, + itemCount: items.length, + reverse: true, + itemBuilder: (BuildContext context, int index) { + return ReorderableDragStartListener( + key: Key('$index'), + index: index, + child: Material( + child: ListTile( + title: Text(items[index]), + ), + ), + ); + }, + ), + ) + ); + + // Start gesture on last item + final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('Item 5'))); + await tester.pump(kLongPressTimeout); + + // Drag to move up the last item, and drop at the last index + await drag.moveBy(const Offset(0, -50)); + await tester.pump(); + await drag.up(); + await tester.pumpAndSettle(); + + expect(tester.takeException(), null); + }); } class TestList extends StatelessWidget {