搜索插件、代码仓与 APP 分类

插件详情v1.2.0

holding_gesture

一个自定义的 GestureDetector,可在按住特定 Widget 时触发操作。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.6万
喜欢数
49
Pub 分数
150

安装插件

flutter pub add holding_gesture

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

holding_gesture 包提供了一个自定义的 GestureDetector,当用户在特定小部件上按住指定时长时触发操作。它扩展了 Flutter 内置的 GestureDetector,具备专用的按住检测功能,可实现响应式且直观的交互,例如长按菜单、拖拽启动或上下文敏感控制。该包轻量且兼容所有主要平台,包括移动设备、网页和桌面端。它简化了基于按住手势的实现,无需编写样板代码。

适合场景

  • 长按上下文菜单
  • 基于手势的导航触发
  • 交互式 widget 激活
  • 拖动开始检测
  • 可自定义的按住持续时间
  • 无障碍增强控件

主要特点

  • 精确的按压持续时间控制
  • 跨平台兼容性
  • 简单的 API 集成
  • 可配置的按住延迟
  • 内置回调处理
  • 最小性能开销

适合对象

  • 开发交互式 UI 的开发者
  • 需要长按功能的应用
  • 跨平台 Flutter 项目
  • 面向可访问性的界面
  • 手势密集型应用

使用建议

  • 需要仔细配置时间
  • 可能与其他手势检测器冲突
  • 不适用于快速点击序列
  • 需要在所有目标平台上进行测试
  • 仅限单个控件按住检测
  • 依赖于 Flutter 手势系统的稳定性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

holding_gesture

A customized GestureDetector that acts on holding a determined Widget.

Getting Started

import 'package:holding_gesture/holding_gesture.dart';
HoldDetector

After importing, all you have to do is use HoldDetector the same way you already used GestureDetector! As it is on the documentation, its onTap override won't work when using a child that has onPressed or a similar callback, so you should pass the single tap behaviour to that.

HoldDetector(
  onHold: _incrementCounter,
  holdTimeout: Duration(milliseconds: 200),
  enableHapticFeedback: true,
  child: ElevatedButton(
    onPressed: _incrementCounter,
    child: Text("onHold"),
  ),
),
HoldTimeoutDetector

HoldTimeoutDetector works differently, you can use it to hold conditions or display something while the user is holding it for a predetermined time, and then execute an action after this duration passes, or canceling it if the hold action ceases. In the example, I'm using it to show a feedback on the button, and on the screen that something is loading, and then executing the increment once the timeout happens.

HoldTimeoutDetector(
  onTimeout: () {
    _incrementCounter();
    _updateLoading(false);
  },
  onTimerInitiated: () => _updateLoading(true),
  onCancel: () => _updateLoading(false),
  holdTimeout: Duration(milliseconds: 3000),
  enableHapticFeedback: true,
  child: ElevatedButton.icon(
    onPressed: () {},
    label: Text("onTimeout"),
    icon: _isLoading
        ? SizedBox(
            width: 16,
            height: 16,
            child: CircularProgressIndicator(
              strokeWidth: 2,
              valueColor: AlwaysStoppedAnimation(Colors.white),
            ),
          )
        : Icon(Icons.timer_3),
  ),
),

The main example, above and in example/, is the same as the default Flutter app, with the adition of a haptic feedback to each tick and the ability to hold the button to keep adding to the counter.

所属分类

源码仓库

使用此插件的项目