Skip to content

Commit 25c5bf0

Browse files
authored
fix SliverReorderableLists item wrong offset (flutter#136828)
This PR fixes the issue of items being created at the wrong position during dragging. Fixes flutter#135819
1 parent 4afdfca commit 25c5bf0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/flutter/lib/src/widgets/reorderable_list.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,9 @@ class SliverReorderableListState extends State<SliverReorderableList> with Ticke
730730
}
731731

732732
void _registerItem(_ReorderableItemState item) {
733+
if (_dragInfo != null && _items[item.index] != item) {
734+
item.updateForGap(_dragInfo!.index, _dragInfo!.itemExtent, false, _reverse);
735+
}
733736
_items[item.index] = item;
734737
if (item.index == _dragInfo?.index) {
735738
item.dragging = true;

packages/flutter/test/widgets/reorderable_list_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,55 @@ void main() {
13561356

13571357
expect(tester.takeException(), null);
13581358
});
1359+
1360+
testWidgetsWithLeakTracking(
1361+
'When creating a new item, be in the correct position',
1362+
(WidgetTester tester) async {
1363+
await tester.pumpWidget(
1364+
MaterialApp(
1365+
home: LayoutBuilder(
1366+
builder: (_, BoxConstraints view) {
1367+
// The third one just appears on the screen
1368+
final double itemSize = view.maxWidth / 2 - 20;
1369+
return Scaffold(
1370+
body: CustomScrollView(
1371+
scrollDirection: Axis.horizontal,
1372+
cacheExtent: 0, // The fourth one will not be created in the initial state.
1373+
slivers: <Widget>[
1374+
SliverReorderableList(
1375+
itemBuilder: (BuildContext context, int index) {
1376+
return ReorderableDragStartListener(
1377+
key: ValueKey<int>(index),
1378+
index: index,
1379+
child: Builder(
1380+
builder: (BuildContext context) {
1381+
return SizedBox(
1382+
width: itemSize,
1383+
child: Text('$index'),
1384+
);
1385+
},
1386+
),
1387+
);
1388+
},
1389+
itemCount: 4,
1390+
onReorder: (int fromIndex, int toIndex) {},
1391+
),
1392+
],
1393+
),
1394+
);
1395+
},
1396+
),
1397+
),
1398+
);
1399+
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('0')));
1400+
await tester.pump(kLongPressTimeout);
1401+
await drag.moveBy(const Offset(20, 0));
1402+
await tester.pump();
1403+
expect(find.text('3').hitTestable(at: Alignment.topLeft), findsNothing);
1404+
await drag.up();
1405+
await tester.pumpAndSettle();
1406+
},
1407+
);
13591408
}
13601409

13611410
class TestList extends StatelessWidget {

0 commit comments

Comments
 (0)