From 43d06c8ea73af97bd6a9c99924f38b191d734a0d Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Wed, 12 May 2021 04:14:03 +0800 Subject: [PATCH] Add draggable parameter to ReorderableDragStartListener (#81396) --- .../lib/src/widgets/reorderable_list.dart | 12 +- .../test/widgets/reorderable_list_test.dart | 191 ++++++++++++++++++ 2 files changed, 201 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/reorderable_list.dart b/packages/flutter/lib/src/widgets/reorderable_list.dart index a074ba5c0b..46f9220df3 100644 --- a/packages/flutter/lib/src/widgets/reorderable_list.dart +++ b/packages/flutter/lib/src/widgets/reorderable_list.dart @@ -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() { diff --git a/packages/flutter/test/widgets/reorderable_list_test.dart b/packages/flutter/test/widgets/reorderable_list_test.dart index 383c668eeb..60727bc19a 100644 --- a/packages/flutter/test/widgets/reorderable_list_test.dart +++ b/packages/flutter/test/widgets/reorderable_list_test.dart @@ -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 items = List.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(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([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 items = List.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(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([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 items = List.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(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([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 items = List.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(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([0, 1, 2, 3, 4])); + }); + }); } class TestList extends StatefulWidget {