nested_reorderable_list
一个Flutter包,提供了一个用于创建嵌套可重排列表的widget。
入门
此包提供了一个无状态widget,NestedReorderableList,它允许您创建一个具有拖放项目重排功能的列表。此widget支持嵌套列表,这意味着列表项可以拥有自己的子项,这些子项也可以被重新排序。
用法
首先,您需要在pubspec.yaml文件中添加nested_reorderable_list
dependencies:
flutter:
sdk: flutter
nested_reorderable_list: ^0.0.1
然后,在您的Dart文件中导入该包
import 'package:nested_reorderable_list/nested_reorderable_list.dart';
以下是如何使用NestedReorderableList widget的基本示例
NestedReorderableList<String>(
dragAndDropItems: items,
itemBuilder: (BuildContext context, DragAndDropItem<String> item) => Text(item.content),
onReorder: (SourceLocation source, DestinationLocation destination, DragAndDropItem<String> movedItem) {
setState(() {
// Determine the source and destination lists based on whether the parentIndex is null
final sourceList = (source.parentIndex == null)
? items
: items[source.parentIndex!].children;
final destList = (destination.parentIndex == null)
? items
: items[destination.parentIndex!].children;
// Perform the removal and insertion operation
final moved = sourceList.removeAt(source.index);
final destinationIndex = destination.index;
destList.insert(destinationIndex, moved);
});
},
);
在上面的示例中,items是一个DragAndDropItem实例列表,每个实例都包含一个用于标识的key,项目的内容,以及子DragAndDropItem实例列表(如果有)。itemBuilder是一个函数,它接收一个BuildContext和一个DragAndDropItem,并返回一个代表该项目的Widget。当拖放操作完成时,会调用onReorder函数;它接收一个SourceLocation和一个DestinationLocation,分别代表被拖动项的初始位置和最终位置。
许可证
本项目根据MIT许可证授权 – 详情请参阅LICENSE文件。
