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

Flutter使用touchable库遇GestureDetector冗余错误求助

解决CanvasTouchDetector的GestureDetector冗余错误

根据你提供的错误信息,问题核心是CanvasTouchDetector内部同时注册了Pan和Scale手势识别器,而Scale手势本身已经包含了Pan(平移)的功能,Flutter因此判定这种配置冗余并抛出错误。

修复方案

直接通过CanvasTouchDetector的gestureRecognizers参数,自定义手势识别器集合,只保留需要的识别器即可:

方案1:保留Scale手势(推荐,因为包含Pan功能)

Container(
  child: FittedBox(
    child: tooth.length == 20 && mouth != null
        ? SizedBox(
            width: mouth?.width.toDouble(),
            height: mouth?.height.toDouble(),
            child: CanvasTouchDetector(
              // 自定义手势识别器,只保留Scale
              gestureRecognizers: {
                Factory<OneSequenceGestureRecognizer>(() => ScaleGestureRecognizer()),
              },
              builder: (context) => CustomPaint(
                painter: FaceOutlinePainter(context),
              ),
            ))
        : Text('data'),
  ),
)

方案2:仅保留Pan手势(如果不需要缩放功能)

Container(
  child: FittedBox(
    child: tooth.length == 20 && mouth != null
        ? SizedBox(
            width: mouth?.width.toDouble(),
            height: mouth?.height.toDouble(),
            child: CanvasTouchDetector(
              // 自定义手势识别器,只保留Pan
              gestureRecognizers: {
                Factory<OneSequenceGestureRecognizer>(() => PanGestureRecognizer()),
              },
              builder: (context) => CustomPaint(
                painter: FaceOutlinePainter(context),
              ),
            ))
        : Text('data'),
  ),
)

额外检查

确认你的FaceOutlinePainter中没有额外手动添加手势识别逻辑,避免重复注册导致冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 17:57:14