Unverified Commit 43d06c8e authored by Valentin Vignal's avatar Valentin Vignal Committed by GitHub

Add draggable parameter to ReorderableDragStartListener (#81396)

parent aa81ac60
......@@ -1050,6 +1050,7 @@ class ReorderableDragStartListener extends StatelessWidget {
Key? key,
required this.child,
required this.index,
this.enabled = true,
}) : super(key: key);
/// The widget for which the application would like to respond to a tap and
......@@ -1059,10 +1060,16 @@ class ReorderableDragStartListener extends StatelessWidget {
/// The index of the associated item that will be dragged in the list.
final int index;
/// Whether the [child] item can be dragged and moved in the list.
///
/// If true, the item can be moved to another location in the list when the
/// user taps on the child. If false, tapping on the child will be ignored.
final bool enabled;
@override
Widget build(BuildContext context) {
return Listener(
onPointerDown: (PointerDownEvent event) => _startDragging(context, event),
onPointerDown: enabled ? (PointerDownEvent event) => _startDragging(context, event) : null,
child: child,
);
}
......@@ -1111,7 +1118,8 @@ class ReorderableDelayedDragStartListener extends ReorderableDragStartListener {
Key? key,
required Widget child,
required int index,
}) : super(key: key, child: child, index: index);
bool enabled = true,
}) : super(key: key, child: child, index: index, enabled: enabled);
@override
MultiDragGestureRecognizer createRecognizer() {
......
......@@ -394,6 +394,197 @@ void main() {
expect(item1Height, 30.0);
expect(item2Height, 30.0);
});
group('ReorderableDragStartListener', () {
testWidgets('It should allow the item to be dragged when enabled is true', (WidgetTester tester) async {
const int itemCount = 5;
int onReorderCallCount = 0;
final List<int> items = List<int>.generate(itemCount, (int index) => index);
void handleReorder(int fromIndex, int toIndex) {
onReorderCallCount += 1;
if (toIndex > fromIndex) {
toIndex -= 1;
}
items.insert(toIndex, items.removeAt(fromIndex));
}
// The list has five elements of height 100
await tester.pumpWidget(
MaterialApp(
home: ReorderableList(
itemCount: itemCount,
itemBuilder: (BuildContext context, int index) {
return SizedBox(
key: ValueKey<int>(items[index]),
height: 100,
child: ReorderableDragStartListener(
child: Text('item ${items[index]}'),
index: index,
),
);
},
onReorder: handleReorder,
),
),
);
// Start gesture on first item
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('item 0')));
await tester.pump(kPressTimeout);
// Drag enough to move down the first item
await drag.moveBy(const Offset(0, 150));
await tester.pump();
await drag.up();
await tester.pumpAndSettle();
expect(onReorderCallCount, 1);
expect(items, orderedEquals(<int>[1, 0, 2, 3, 4]));
});
testWidgets('It should allow the item to be dragged when enabled is true', (WidgetTester tester) async {
const int itemCount = 5;
int onReorderCallCount = 0;
final List<int> items = List<int>.generate(itemCount, (int index) => index);
void handleReorder(int fromIndex, int toIndex) {
onReorderCallCount += 1;
if (toIndex > fromIndex) {
toIndex -= 1;
}
items.insert(toIndex, items.removeAt(fromIndex));
}
// The list has five elements of height 100
await tester.pumpWidget(
MaterialApp(
home: ReorderableList(
itemCount: itemCount,
itemBuilder: (BuildContext context, int index) {
return SizedBox(
key: ValueKey<int>(items[index]),
height: 100,
child: ReorderableDragStartListener(
child: Text('item ${items[index]}'),
index: index,
enabled: false,
),
);
},
onReorder: handleReorder,
),
),
);
// Start gesture on first item
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('item 0')));
await tester.pump(kLongPressTimeout);
// Drag enough to move down the first item
await drag.moveBy(const Offset(0, 150));
await tester.pump();
await drag.up();
await tester.pumpAndSettle();
expect(onReorderCallCount, 0);
expect(items, orderedEquals(<int>[0, 1, 2, 3, 4]));
});
});
group('ReorderableDelayedDragStartListener', () {
testWidgets('It should allow the item to be dragged when enabled is true', (WidgetTester tester) async {
const int itemCount = 5;
int onReorderCallCount = 0;
final List<int> items = List<int>.generate(itemCount, (int index) => index);
void handleReorder(int fromIndex, int toIndex) {
onReorderCallCount += 1;
if (toIndex > fromIndex) {
toIndex -= 1;
}
items.insert(toIndex, items.removeAt(fromIndex));
}
// The list has five elements of height 100
await tester.pumpWidget(
MaterialApp(
home: ReorderableList(
itemCount: itemCount,
itemBuilder: (BuildContext context, int index) {
return SizedBox(
key: ValueKey<int>(items[index]),
height: 100,
child: ReorderableDelayedDragStartListener(
child: Text('item ${items[index]}'),
index: index,
),
);
},
onReorder: handleReorder,
),
),
);
await tester.pumpAndSettle();
// Start gesture on first item
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('item 0')));
await tester.pump(kLongPressTimeout);
// Drag enough to move down the first item
await drag.moveBy(const Offset(0, 50));
await tester.pump();
await drag.up();
await tester.pumpAndSettle();
expect(onReorderCallCount, 1);
expect(items, orderedEquals(<int>[1, 0, 2, 3, 4]));
});
testWidgets('It should allow the item to be dragged when enabled is true', (WidgetTester tester) async {
const int itemCount = 5;
int onReorderCallCount = 0;
final List<int> items = List<int>.generate(itemCount, (int index) => index);
void handleReorder(int fromIndex, int toIndex) {
onReorderCallCount += 1;
if (toIndex > fromIndex) {
toIndex -= 1;
}
items.insert(toIndex, items.removeAt(fromIndex));
}
// The list has five elements of height 100
await tester.pumpWidget(
MaterialApp(
home: ReorderableList(
itemCount: itemCount,
itemBuilder: (BuildContext context, int index) {
return SizedBox(
key: ValueKey<int>(items[index]),
height: 100,
child: ReorderableDelayedDragStartListener(
child: Text('item ${items[index]}'),
index: index,
enabled: false,
),
);
},
onReorder: handleReorder,
),
),
);
// Start gesture on first item
final TestGesture drag = await tester.startGesture(tester.getCenter(find.text('item 0')));
await tester.pump(kLongPressTimeout);
// Drag enough to move down the first item
await drag.moveBy(const Offset(0, 50));
await tester.pump();
await drag.up();
await tester.pumpAndSettle();
expect(onReorderCallCount, 0);
expect(items, orderedEquals(<int>[0, 1, 2, 3, 4]));
});
});
}
class TestList extends StatefulWidget {
......
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