flutter 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,
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      )


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值