如何使用Flutter在移动应用程序中禁用多点触摸

时间:2018-08-06 16:49:01

标签: dart flutter multi-touch

这个问题完全基于GestureDetector颤动。

例如:在应用程序中,实现了GestureDetector类,因此在默认情况下,它支持多点触摸,现在需要禁用此多点触摸,因此这可能是解决方案的最佳方法。

GestureDetector参考链接:https://docs.flutter.io/flutter/widgets/GestureDetector-class.html

3 个答案:

答案 0 :(得分:3)

通过使用InstantMultiDragGestureRecognizer()解决的问题,以下代码显示了如何使用它。

child: RawGestureDetector(
              behavior: HitTestBehavior.opaque,
              gestures: <Type, GestureRecognizerFactory>{
                ImmediateMultiDragGestureRecognizer:
                    GestureRecognizerFactoryWithHandlers<
                        ImmediateMultiDragGestureRecognizer>(
                  () => ImmediateMultiDragGestureRecognizer(),
                  (ImmediateMultiDragGestureRecognizer instance) {
                    instance..onStart = _handleOnStart;
                  },
                ),
              },
    Drag _handleOnStart(Offset position) {
   if (count < 1) {
     setState(() {
       count++;
     });
     return _DragHandler(_handleDragUpdate, _handleDragEnd);
}
return null;
  }

  void _handleDragUpdate(DragUpdateDetails update) {
    //code is here
  }

  void _handleDragEnd(DragEndDetails details) {
    //code is here
   }
   setState(() {
    count = 0;
   });
   }


   class _DragHandler extends Drag {
  _DragHandler(this.onUpdate, this.onEnd);

  final GestureDragUpdateCallback onUpdate;
  final GestureDragEndCallback onEnd;

  @override
  void update(DragUpdateDetails details) {
   onUpdate(details);
  }

  @override
  void end(DragEndDetails details) {
    onEnd(details);
}
@override
void cancel(){}
}

答案 1 :(得分:2)

创建OnlyOnePointerRecognizerWidget小部件的实例,并将任何小部件作为子级传递给它。 OnlyOnePointerRecognizerWidget将仅识别一个指针。

import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';

class OnlyOnePointerRecognizer extends OneSequenceGestureRecognizer {
  int _p = 0;
  @override
  void addPointer(PointerDownEvent event) {
    startTrackingPointer(event.pointer);
    if (_p == 0) {
      resolve(GestureDisposition.rejected);
      _p = event.pointer;
    } else {
      resolve(GestureDisposition.accepted);
    }
  }

  @override
  String get debugDescription => 'only one pointer recognizer';

  @override
  void didStopTrackingLastPointer(int pointer) {}

  @override
  void handleEvent(PointerEvent event) {
    if (!event.down && event.pointer == _p) {
      _p = 0;
    }
  }
}

class OnlyOnePointerRecognizerWidget extends StatelessWidget {
  final Widget child;
  OnlyOnePointerRecognizerWidget({this.child});
  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
      gestures: <Type, GestureRecognizerFactory>{
        OnlyOnePointerRecognizer: GestureRecognizerFactoryWithHandlers<OnlyOnePointerRecognizer>(
          () => OnlyOnePointerRecognizer(),
          (OnlyOnePointerRecognizer instance) {},
        ),
      },
      child: child,
    );
  }
}

答案 2 :(得分:1)

听起来像您想要一个MultiDragGestureRecognizer。您需要创建一个实例化MultiDragGestureRecognizer的StatefulWidget,然后使您的构建函数具有一个侦听器,该侦听器将onPointerDown事件路由到识别器。 我们可能会在GestureDetector本身中添加该识别器,或者提供包装该识别器的小部件(如果这是人们经常做的事情)。 更重要的是,我们可能应该对此进行记录。我为此保留该错误。 flutter gestures library