Skip to content

Commit eefed11

Browse files
Fix gap flipping when dragging variable height reorderable items (flutter#194478)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> ## Description When dragging an item in a SliverReorderableList containing items of different heights, the gap can flicker between positions as the pointer moves in small increments. This results in unstable reorder animations. ## Changes - Skip the currently dragged item when calculating the insertion index to prevent its original position from interfering with the placement of the gap. - Remove the special handling for the dragged item in the normal-direction insertion logic. - Add a regression test using variable-height items and incremental pointer movements to verify that the gap remains stable during dragging. ## Testing - Targeted Regression Test: `flutter test packages/flutter/test/widgets/reorderable_list_test.dart --plain-name "SliverReorderableList keeps gap stable when dragging variable height items"` - Full Test Suite: `flutter test packages/flutter/test/widgets/reorderable_list_test.dart` (All 50 tests pass) - Code Hygiene: `git diff --check` passed cleanly. ## Issue flutter#194436 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent feab0e0 commit eefed11

2 files changed

Lines changed: 70 additions & 8 deletions

File tree

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,9 @@ class SliverReorderableListState extends State<SliverReorderableList>
10531053
// Find the new index for inserting the item being dragged.
10541054
int newIndex = _insertIndex!;
10551055
for (final _ReorderableItemState item in _items.values) {
1056-
if ((_reverse && item.index == _dragIndex!) || !item.mounted) {
1056+
// Skip the dragged item so its original slot does not compete
1057+
// with the other items when determining the insertion index.
1058+
if (item.index == _dragIndex! || !item.mounted) {
10571059
continue;
10581060
}
10591061

@@ -1084,13 +1086,7 @@ class SliverReorderableListState extends State<SliverReorderableList>
10841086
newIndex = item.index;
10851087
}
10861088
} else {
1087-
if (item.index == _dragIndex!) {
1088-
// If end of the proxy is not in ending half of item,
1089-
// we don't process, because it's original dragged item.
1090-
if (itemMiddle <= proxyItemEnd && proxyItemEnd <= itemEnd) {
1091-
newIndex = _dragIndex!;
1092-
}
1093-
} else if (itemStart <= proxyItemStart && proxyItemStart <= itemMiddle) {
1089+
if (itemStart <= proxyItemStart && proxyItemStart <= itemMiddle) {
10941090
// The start of the proxy is in the beginning half of the item, so
10951091
// we should swap the item with the gap and we are done looking for
10961092
// the new index.

‎packages/flutter/test/widgets/reorderable_list_test.dart‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,72 @@ void main() {
21712171
);
21722172
expect(tester.getSize(find.byType(ReorderableDelayedDragStartListener)), Size.zero);
21732173
});
2174+
2175+
testWidgets(
2176+
'SliverReorderableList gap does not flip when dragging a taller item over a shorter one',
2177+
(WidgetTester tester) async {
2178+
// Regression test for https://github.com/flutter/flutter/issues/194436.
2179+
const itemHeights = <double>[68, 85, 126, 97, 54, 106, 110, 119];
2180+
final items = List<int>.generate(itemHeights.length, (int index) => index);
2181+
2182+
await tester.pumpWidget(
2183+
TestWidgetsApp(
2184+
home: StatefulBuilder(
2185+
builder: (BuildContext context, StateSetter setState) {
2186+
return CustomScrollView(
2187+
slivers: <Widget>[
2188+
SliverReorderableList(
2189+
itemCount: items.length,
2190+
itemBuilder: (BuildContext context, int index) {
2191+
final int item = items[index];
2192+
return SizedBox(
2193+
key: ValueKey<int>(item),
2194+
height: itemHeights[item],
2195+
child: ReorderableDragStartListener(
2196+
index: index,
2197+
child: Text('item $item'),
2198+
),
2199+
);
2200+
},
2201+
onReorderItem: (int fromIndex, int toIndex) {
2202+
setState(() {
2203+
items.insert(toIndex, items.removeAt(fromIndex));
2204+
});
2205+
},
2206+
),
2207+
],
2208+
);
2209+
},
2210+
),
2211+
),
2212+
);
2213+
2214+
final Finder item4 = find.text('item 4');
2215+
final Finder item5 = find.text('item 5');
2216+
expect(tester.getTopLeft(item4).dy, 376.0);
2217+
expect(tester.getTopLeft(item5).dy, 430.0);
2218+
2219+
// Pick up item 5 (106px) and move it up into the top half of item 4 (54px).
2220+
final TestGesture drag = await tester.startGesture(tester.getCenter(item5));
2221+
await tester.pump(kPressTimeout);
2222+
await drag.moveBy(const Offset(0, -48.5));
2223+
await tester.pumpAndSettle();
2224+
2225+
// Item 4 is pushed down by item 5's extent: 376 + 106.
2226+
final double pushedDown = tester.getTopLeft(item4).dy;
2227+
expect(pushedDown, 482.0);
2228+
2229+
// Creep upward half a pixel at a time; item 4 must not flip back to 376.
2230+
for (var move = 0; move < 8; move++) {
2231+
await drag.moveBy(const Offset(0, -0.5));
2232+
await tester.pumpAndSettle();
2233+
expect(tester.getTopLeft(item4).dy, pushedDown);
2234+
}
2235+
2236+
await drag.up();
2237+
await tester.pumpAndSettle();
2238+
},
2239+
);
21742240
}
21752241

21762242
class TestList extends StatelessWidget {

0 commit comments

Comments
 (0)