'How to Disable ReorderableListView Reorder capability but still allow scrolling?
How do we deactivate the ReorderableListView dragging/reordering capabilities in Flutter?
I would like to disable the dragging function but still allow scrolling.
Any ideas? Thank you
Solution 1:[1]
You could wrap your list item in an IgnorePointer widget and control with a bool, like this
class ReorderableList extends StatefulWidget {
@override
_ReorderableListState createState() => _ReorderableListState();
}
class _ReorderableListState extends State<ReorderableList> {
bool _allowReorder = true;
final List<int> _items = List<int>.generate(50, (int index) => index);
void reorderData(int oldindex, int newindex) {
setState(() {
if (oldindex < newindex) {
newindex -= 1;
}
final int item = _items.removeAt(oldindex);
_items.insert(newindex, item);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(onPressed: () {
setState(() {
_allowReorder = !_allowReorder;
});
}),
body: SafeArea(
child: Column(
children: [
Expanded(
child: ReorderableListView(
onReorder: (one, two) => reorderData(one, two),
children: <Widget>[
for (int index = 0; index < _items.length; index++)
if (_allowReorder)
ListTile(
key: Key('$index'),
title: Text('Item ${_items[index]}'),
)
else
IgnorePointer(
key: Key('$index'),
child: ListTile(
title: Text('Item ${_items[index]}'),
),
)
],
),
),
],
),
),
);
}
}
Solution 2:[2]
One way to disable reordering is by setting parameter buildDefaultDragHandles of ReorderableListView.builder() to false.
For example, if you'd like to enable reordering only if there are at least two items in the list, you could do something like:
ReorderableListView.builder(
itemCount: someList.length,
itemBuilder: _buildListWidget,
buildDefaultDragHandles: someList.length > 1, // disable drag&drop if there's only one item
onReorder: onReorderList,
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
| Solution 2 | SePröbläm |
