我们如何停用 Flutter 中的 ReorderableListView 拖动/重新排序功能?
我想禁用拖动功能但仍然允许滚动。
有任何想法吗?谢谢
您可以将列表项包装在 IgnorePointer 小部件中并使用 bool 进行控制,如下所示
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]}'),
),
)
],
),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1901 次 |
| 最近记录: |