我正在使用带有SliverChildBuilderDelegate的SliverList来动态生成列表项。现在我试图允许用户通过拖放一行中每个项目上的句柄图标来重新排序列表项。
我尝试了不同的东西(比如Draggable Widget)但到目前为止我还没找到解决办法。有没有人已经使用SliverList Widget进行拖放重新排序并且可以给我一个提示?
使用ReorderableListView Widget是不可能的,导致将ListView混合到SliverList。我想使用SliverAppBar来实现淡出滚动效果,如下所示:https://medium.com/flutter-io/slivers-demystified-6ff68ab0296f
这是我的SliverList的结构:
return Scaffold(
body: RefreshIndicator(
...
child: CustomScrollView(
...
slivers: <Widget>[
SliverAppBar(...),
SliverList(
delegate: SliverChildBuilderDelegate(...),
)
...
迈克尔,提前和最好的谢谢
你可以查看flutter_reorderable_list。我还没有尝试过它,目前正在进行自己的推广,但它看起来不错,the example使用带有SliverList的CustomScrollView。
看看这个酒吧的reorderables套餐。它最近增加了对SliverList的支持。
该示例包含条子列表和应用栏,并显示您要查找的内容。只需使用包中的计数器部件替换代码中的SliverList和SliverChildBuilderDelegate即可。
class _SliverExampleState extends State<SliverExample> {
List<Widget> _rows;
@override
void initState() {
super.initState();
_rows = List<Widget>.generate(50,
(int index) => Text('This is sliver child $index', textScaleFactor: 2)
);
}
@override
Widget build(BuildContext context) {
void _onReorder(int oldIndex, int newIndex) {
setState(() {
Widget row = _rows.removeAt(oldIndex);
_rows.insert(newIndex, row);
});
}
ScrollController _scrollController = PrimaryScrollController.of(context) ?? ScrollController();
return CustomScrollView(
// a ScrollController must be included in CustomScrollView, otherwise
// ReorderableSliverList wouldn't work
controller: _scrollController,
slivers: <Widget>[
SliverAppBar(
expandedHeight: 210.0,
flexibleSpace: FlexibleSpaceBar(
title: Text('ReorderableSliverList'),
background: Image.network(
'https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Yushan'
'_main_east_peak%2BHuang_Chung_Yu%E9%BB%83%E4%B8%AD%E4%BD%91%2B'
'9030.png/640px-Yushan_main_east_peak%2BHuang_Chung_Yu%E9%BB%83'
'%E4%B8%AD%E4%BD%91%2B9030.png'),
),
),
ReorderableSliverList(
delegate: ReorderableSliverChildListDelegate(_rows),
// or use ReorderableSliverChildBuilderDelegate if needed
// delegate: ReorderableSliverChildBuilderDelegate(
// (BuildContext context, int index) => _rows[index],
// childCount: _rows.length
// ),
onReorder: _onReorder,
)
],
);
}
}