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

flutter_slidable设置closeWhenOpened:true不生效如何解决

flutter_slidable 1.3.0 多Slidable同时展开问题修复

问题根因

配置不生效核心是SlidableAutoCloseBehavior的使用方式错误:

  • 你给每个列表项单独包裹了一个独立的SlidableAutoCloseBehavior实例,不同实例下的Slidable分属不同的状态监听组,无法互相感知展开状态,自然不会触发其他项的自动关闭。
  • 额外检查到你的配置片段里组件名存在拼写错误:正确组件名是SlidableAutoCloseBehavior,不是SlideableAutoCloseBehavior,拼写错误也会导致组件属性不生效。

修复步骤

  1. 将SlidableAutoCloseBehavior上移到所有列表项的共同父级位置(比如包裹整个ListView/GridView),保证所有需要互斥展开的Slidable都在同一个Behavior的子树范围内。
    示例代码:
// 列表页面父组件中配置
SlidableAutoCloseBehavior(
  closeWhenOpened: true,
  closeWhenTapped: true,
  child: ListView.builder(
    itemCount: mapDataList.length,
    itemBuilder: (context, index) => _mapButton(mapData: mapDataList[index]),
  ),
)
  1. 移除_mapButton组件内部单独包裹的SlidableAutoCloseBehavior,列表项中仅保留Slidable组件本身即可,修正后的_mapButton代码:
class _mapButton extends StatelessWidget {
  const _mapButton({
    Key? key,
    required this.mapData,
  }) : super(key: key);

  final MapData mapData;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(10),
      child: TCard(
        child: Container(
          width: double.infinity,
          child: Slidable(
            closeOnScroll: false,
            endActionPane: ActionPane(
              motion: const ScrollMotion(),
              children: [
                SlidableAction(
                  icon: Icons.delete_outline,
                  label: "삭제",
                  backgroundColor: Colors.red,
                  onPressed: (context) {
                    print(mapData.id);
                  },
                ),
                SlidableAction(
                  icon: Icons.edit_outlined,
                  label: "수정",
                  backgroundColor: TColor.backColor,
                  onPressed: (context) {
                    print(mapData.id);
                  },
                ),
              ],
            ),
            child: Container(
              width: double.infinity,
              child: InkWell(
                onTap: () {
                  print("클릭");
                },
                child: Container(
                  padding: const EdgeInsets.all(10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "data",
                        style: TText.style.titleLarge,
                      ),
                      const SizedBox(height: 10),
                      Text(
                        "data",
                        style: TText.style.titleSmall,
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

补充说明:SlidableAutoCloseBehavior的作用域是自身的整个子树,只有同属一个子树下的Slidable才会被纳入互斥展开的管理范围,单独给每个item包裹Behavior相当于给每个Slidable建了独立的管理组,组内仅有一个Slidable,互斥逻辑自然不会生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 02:54:40