背景
ListView本身没办法进行移动ListTile,所以使用ReorderableListView
例子
class Demo extends StatefulWidget {
@override
_DemoState createState() => _DemoState();
}
class _DemoState extends State<Demo> {
List<int> items = [];
@override
void initState() {
super.initState();
for (int i = 0; i < 10; i++) {
items.add(i);
}
}
@override
Widget build(BuildContext context) {
return ReorderableListView(
children: items.map(toWidget).toList(),
header: Text('header'),
onReorder: (oldIndex, newIndex) {
setState(() {
switchItem(oldIndex, newIndex);
});
});
}
void switchItem(int oldIndex, int newIndex) {
tmp = list[oldIndex];
list[oldIndex] = list[newIndex];
list[newIndex] = tmp;
}
Widget f(int i) {
return ListTile(
key: ValueKey("item $i"),
title: Text('item $i'),
);
}
}
网友评论