You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

flutter_slidable实现列表项完全滑动后执行任务而非删除

解决方案

要实现完全滑动列表项后执行指定任务、不删除项且任务完成后取消滑动状态,你可以通过SlidableController手动控制滑动面板的关闭,具体修改如下:

child: SlidableAutoCloseBehavior(
  child: ListView.separated(
    itemBuilder: ((context, index) {
      // 为每个Slidable创建独立的控制器
      final slidableController = SlidableController();
      return Slidable(
        closeOnScroll: true,
        key: UniqueKey(),
        controller: slidableController, // 绑定控制器到当前Slidable
        startActionPane: ActionPane(
          dismissible: DismissiblePane(
            confirmDismiss: () async {
              // 返回false,阻止滑动到底后自动删除列表项
              return false;
            },
            onDismissed: () async {
              // 执行你的指定任务(这里以Toast为例,可替换为业务逻辑)
              Fluttertoast.showToast(msg: "Full swipe ${index + 1}");
              
              // 如果是异步任务,需等待任务完成后再关闭滑动状态
              // 示例:模拟1秒异步操作
              await Future.delayed(const Duration(seconds: 1));
              
              // 任务完成后,关闭滑动面板,恢复列表项初始状态
              slidableController.close();
            },
          ),
          motion: const BehindMotion(),
          children: [
            SlidableAction(
              onPressed: (BuildContext context) {
                Fluttertoast.showToast(msg: "Unlock ${index + 1}");
              },
              backgroundColor: Colors.red,
              label: "Unlock",
              icon: Icons.lock_open,
            ),
            SlidableAction(
              onPressed: (BuildContext context) {
                Fluttertoast.showToast(msg: "Lock ${index + 1}");
              },
              backgroundColor: Colors.green,
              label: "Lock",
              icon: Icons.lock_outline,
            ),
          ],
        ),
        child: listViewItem(index),
      );
    }),
    separatorBuilder: (BuildContext context, int index) => const Divider(),
    itemCount: list.length,
  ),
)

关键改动说明

  • SlidableController:为每个列表项的Slidable实例创建独立控制器,用于后续手动触发滑动面板的关闭操作。
  • confirmDismiss返回false:阻止DismissiblePane在滑动到底后自动移除列表项,保留原列表项的显示。
  • 任务完成后调用close():在onDismissed中执行完业务任务(异步任务需await等待完成),再调用控制器的close()方法,将滑动面板恢复到初始状态。

内容的提问来源于stack exchange,提问作者Mohammad Taqi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 15:00:45