flutter ReorderableListView 拖拽列表

ReorderableListView示例:实现应用列表拖拽排序
本文介绍了如何在Flutter中使用ReorderableListView构建一个可拖动排序的应用列表,展示了如何设置header、监听reorder事件以及自定义拖拽手势。
//需要渲染的数据
  List rankAppList = ["11111", "22222", "33333", "566577453"];

///文中用到的单位rpx为适配单位,不需要的可以删掉单位,需要的可以看我以前的文章

	Container(
        margin: EdgeInsets.only(top: 24.rpx),
        child: ReorderableListView.builder(
          shrinkWrap: true,
          itemCount: rankAppList.length,
          physics: const NeverScrollableScrollPhysics(),
          /// header参数可以放一些需要的widget,
          header: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
            ],
          ),
          onReorder: (int oldIndex, int newIndex) {
            var oldIndexChild = rankAppList.removeAt(oldIndex);
            if(oldIndex < newIndex){
              ///往下拉。这里往下拉的索引需要➖ 1
              rankAppList.insert(newIndex - 1, oldIndexChild);
            }else{
              ///往上拉
              rankAppList.insert(newIndex, oldIndexChild);
            }
            setState(() {});
          },
          /// buildDefaultDragHandles参数设置为false就可以自己来定义拖拽的按钮,默认为true,整个item都可拖拽
          buildDefaultDragHandles: false,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              height: 60.rpx,
              alignment: Alignment.center,
              /// 需要设置key值
              key: ObjectKey(rankAppList[index]),
              margin: EdgeInsets.only(bottom: 12.rpx,top: 12.rpx),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        width: 34.rpx,
                        height: 34.rpx,
                        alignment: Alignment.center,
                        decoration: const BoxDecoration(
                            shape: BoxShape.circle,
                            color: Color(0xff1A71FF)),
                        child: Text(
                          "${index + 1}",
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 24.rpx,
                              fontWeight: FontWeight.w400),
                        ),
                      ),
                      SizedBox(
                        width: 15.rpx,
                      ),
                      Text(rankAppList[index])
                    ],
                  ),
                  /// 配合buildDefaultDragHandles参数使用 包裹想要拖拽的组件
                  ReorderableDragStartListener(
                    index: index,
                    child: Image.asset(
                      "lib/images/chage_sequence.png",
                      width: 40.rpx,
                      height: 32.rpx,
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      )


Flutter 的嵌套列表拖拽通常是通过使用第三方库如 `flutter_list_view` 或 `dragula` 实现的,这类库允许你在 Flutter 应用中创建可以交互的、可拖动的列表结构。嵌套列表拖拽功能通常用于能够调整层级结构的应用场景,比如文件管理、任务清单等。 在 `flutter_list_view` 中,你可以使用 `NestedListView.builder` 来构建嵌套列表,并利用其提供的 `Draggable` 和 `DropTarget` 组件来进行元素的拖放操作。首先,你需要设置每个列表项为可拖动的 `Draggable`,然后指定目标列表的 `DropTarget` 来处理放置事件。 示例代码可能如下: ```dart import 'package:flutter/material.dart'; import 'package:flutter_list_view/flutter_list_view.dart'; class NestedListDraggable extends StatelessWidget { final Widget itemBuilder; final List draggableItems; NestedListDraggable({required this.itemBuilder, required this.draggableItems}); @override Widget build(BuildContext context) { return Draggable( child: itemBuilder(context), feedback: FeedbackType.circle, childWhenDragging: Container(color: Colors.transparent), data: draggableItems[0], onDragEnd: (DraggableDetails details) { // 更新数据模型,反映拖拽后的状态 }, ); } } // 使用示例 class MyNestedListView extends StatefulWidget { @override _MyNestedListViewState createState() => _MyNestedListViewState(); } class _MyNestedListViewState extends State<MyNestedListView> { List<List<dynamic>> nestedData = [ ['Item 1', ['Subitem 1', 'Subitem 2']], ['Item 2'], ]; void performDrop(DraggableDetails details) { setState(() { List<dynamic> droppedParent = details.pickedObject; int index = details.dropLocation.index; nestedData[index] = droppedParent; // 插入到指定位置 }); } @override Widget build(BuildContext context) { return ListView.builder( itemCount: nestedData.length, itemBuilder: (_, int index) { if (nestedData[index].isNotEmpty && nestedData[index].isInstanceOf<List>) { return NestedListView( itemBuilder: (context, child) { return Draggable( child: ListTile(title: Text(nestedData[index][0])), // 其他配置... onDragEnd: performDrop, ); }, children: nestedData[index], ); } else { return ListTile(title: Text(nestedData[index].toString())); } }, ); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值