Unverified Commit aa0382e9 authored by Alberto Monteiro's avatar Alberto Monteiro Committed by GitHub

ReorderableListView should not reorder if there is only a single item present (#59631)

parent 99f5eebc
...@@ -574,6 +574,11 @@ class _ReorderableListContentState extends State<_ReorderableListContent> with T ...@@ -574,6 +574,11 @@ class _ReorderableListContentState extends State<_ReorderableListContent> with T
); );
break; break;
} }
// If the reorderable list only has one child element, reordering
// should not be allowed.
final bool hasMoreThanOneChildElement = widget.children.length > 1;
return SingleChildScrollView( return SingleChildScrollView(
scrollDirection: widget.scrollDirection, scrollDirection: widget.scrollDirection,
padding: widget.padding, padding: widget.padding,
...@@ -581,10 +586,10 @@ class _ReorderableListContentState extends State<_ReorderableListContent> with T ...@@ -581,10 +586,10 @@ class _ReorderableListContentState extends State<_ReorderableListContent> with T
reverse: widget.reverse, reverse: widget.reverse,
child: _buildContainerForScrollDirection( child: _buildContainerForScrollDirection(
children: <Widget>[ children: <Widget>[
if (widget.reverse) _wrap(finalDropArea, widget.children.length, constraints), if (widget.reverse && hasMoreThanOneChildElement) _wrap(finalDropArea, widget.children.length, constraints),
if (widget.header != null) widget.header, if (widget.header != null) widget.header,
for (int i = 0; i < widget.children.length; i += 1) _wrap(widget.children[i], i, constraints), for (int i = 0; i < widget.children.length; i += 1) _wrap(widget.children[i], i, constraints),
if (!widget.reverse) _wrap(finalDropArea, widget.children.length, constraints), if (!widget.reverse && hasMoreThanOneChildElement) _wrap(finalDropArea, widget.children.length, constraints),
], ],
), ),
); );
......
...@@ -56,6 +56,33 @@ void main() { ...@@ -56,6 +56,33 @@ void main() {
}); });
group('in vertical mode', () { group('in vertical mode', () {
testWidgets('reorder is not triggered when children length is less or equals to 1', (WidgetTester tester) async {
bool onReorderWasCalled = false;
final List<String> currentListItems = listItems.take(1).toList();
final ReorderableListView reorderableListView = ReorderableListView(
header: const Text('Header'),
children: currentListItems.map<Widget>(listItemToWidget).toList(),
scrollDirection: Axis.vertical,
onReorder: (_, __) => onReorderWasCalled = true,
);
final List<String> currentOriginalListItems = originalListItems.take(1).toList();
await tester.pumpWidget(MaterialApp(
home: SizedBox(
height: itemHeight * 10,
child: reorderableListView,
),
));
expect(currentListItems, orderedEquals(currentOriginalListItems));
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('Item 1')));
await tester.pump(kLongPressTimeout + kPressTimeout);
expect(currentListItems, orderedEquals(currentOriginalListItems));
await drag.moveTo(tester.getBottomLeft(find.text('Item 1')) * 2);
expect(currentListItems, orderedEquals(currentOriginalListItems));
await drag.up();
expect(onReorderWasCalled, false);
expect(currentListItems, orderedEquals(<String>['Item 1']));
});
testWidgets('reorders its contents only when a drag finishes', (WidgetTester tester) async { testWidgets('reorders its contents only when a drag finishes', (WidgetTester tester) async {
await tester.pumpWidget(build()); await tester.pumpWidget(build());
expect(listItems, orderedEquals(originalListItems)); expect(listItems, orderedEquals(originalListItems));
...@@ -547,6 +574,33 @@ void main() { ...@@ -547,6 +574,33 @@ void main() {
}); });
group('in horizontal mode', () { group('in horizontal mode', () {
testWidgets('reorder is not triggered when children length is less or equals to 1', (WidgetTester tester) async {
bool onReorderWasCalled = false;
final List<String> currentListItems = listItems.take(1).toList();
final ReorderableListView reorderableListView = ReorderableListView(
header: const Text('Header'),
children: currentListItems.map<Widget>(listItemToWidget).toList(),
scrollDirection: Axis.horizontal,
onReorder: (_, __) => onReorderWasCalled = true,
);
final List<String> currentOriginalListItems = originalListItems.take(1).toList();
await tester.pumpWidget(MaterialApp(
home: SizedBox(
height: itemHeight * 10,
child: reorderableListView,
),
));
expect(currentListItems, orderedEquals(currentOriginalListItems));
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('Item 1')));
await tester.pump(kLongPressTimeout + kPressTimeout);
expect(currentListItems, orderedEquals(currentOriginalListItems));
await drag.moveTo(tester.getBottomLeft(find.text('Item 1')) * 2);
expect(currentListItems, orderedEquals(currentOriginalListItems));
await drag.up();
expect(onReorderWasCalled, false);
expect(currentListItems, orderedEquals(<String>['Item 1']));
});
testWidgets('allows reordering from the very top to the very bottom', (WidgetTester tester) async { testWidgets('allows reordering from the very top to the very bottom', (WidgetTester tester) async {
await tester.pumpWidget(build(scrollDirection: Axis.horizontal)); await tester.pumpWidget(build(scrollDirection: Axis.horizontal));
expect(listItems, orderedEquals(originalListItems)); expect(listItems, orderedEquals(originalListItems));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment