如果你曾用Flutter构建过一些东西,你可能已经使用过了ListView widget
他们可以调整,显示和滚动项目列表。小菜一碟~
但是,你不能做的一件事就是在列表中移动项目,幸运的是,有ReorderableListView。
有了它,用户可以长按项目,在ListView滚动方向移动它,然后将其放在新位置。
为了使这项工作进展顺利,ReorderableListView获得子窗口widget 列表。ListTitle运作的很好,
RecorderableListView(
children: [
for(final item in myItems) {
ListTitle(
key: ValueKey(item),
title: Text('Item #$item'),
),
}
]
)
加上在用户重新排序列表时调用的onReorder回调。
你可以使用它来更新支持ReorderableListView的项目列表。
如果你有一个窗口widget,应该出现在项目上方的,确保每一个子窗口widget都有一个特定的key,以便ReorderableListView可以识别它们并使用可选的正体属性。
RecorderableListView(
onReorder: (oldIndex, newIndex) {
setState((){
_updateMyItems(oldIndex, newIntex);
}
);
},
header: Text('This is the header!'),
children: [
for(final item in myItems) {
ListTitle(
key: ValueKey(item),
title: Text('Item #$item'),
),
}
]
)
如果想了解有关ReorderableListView的内容,或者关于Flutter的其他功能,请访问flutter.dev
原文翻译自视频:视频地址