文章目录
前言
在上一篇《双指缩放和双指移动共存手势系列–1方案》中通过直接在GestureDetector 中加入手势判断代码来实现是双指缩放或是双指触屏移动控件。上一篇也提到个问题,就是该代码与功能代码高度耦合,不利于是多项目之间共享。
本篇讲把该部分代码进行封装,作为一个 package 独立存在。
一、封装效果

如上图所示,左侧是封装后的控件,右侧是GestureDetector 控件。左侧控件在使用上保持了 GestureDetector 的一致性。新控件GestureDetectorTwoFingersScaleMov多了一个controller, 该controller 主要是用于存储手势区分数据。
二、GestureDetector 深入了解
1.GestureDetector对象结构
如上图GestureDetector 将所需要的GestureRecognizer 进行组装,然后调用更为底层的RawGestureDetector 在该组件中完成手势检测。并回调GestureDetector 中注册的回调函数。
2.手势回调
3. Gesture Detector 代码片段
Widget build(BuildContext context) {
final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{
};
final DeviceGestureSettings? gestureSettings = MediaQuery.maybeGestureSettingsOf(context);
...
if (onPanDown != null ||
onPanStart != null ||
onPanUpdate != null ||
onPanEnd != null ||
onPanCancel != null) {
gestures[PanGestureRecognizer] = GestureRecognizerFactoryWithHandlers<PanGestureRecognizer>(
() => PanGestureRecognizer

最低0.47元/天 解锁文章





