How to disable multi-touch in mobile application using flutter

徘徊边缘 提交于 2019-11-28 03:37:15

问题


This question is purely based on GestureDetector flutter.

For Example: In Application, GestureDetector class is implemented so here by-default it support multi-touch, now need to disable this multi-touch so what could be the best way of a solution.

GestureDetector reference link: https://docs.flutter.io/flutter/widgets/GestureDetector-class.html


回答1:


Create instance of OnlyOnePointerRecognizerWidget widget and pass any Widget as child to it. OnlyOnePointerRecognizerWidget will recognize only one pointer.

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:


Issue fixed by using ImmediateMultiDragGestureRecognizer() the below code shows how we can use it.

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(){}
}



回答3:


It sounds like you want a MultiDragGestureRecognizer. You'll need to create a StatefulWidget that instantiates the MultiDragGestureRecognizer, then have your build function have a Listener that routes the onPointerDown event to the recognizer. We could probably add that recognizer to GestureDetector itself, or provide a widget that wraps that recognizer, if it's something people do commonly. More importantly, we should probably document this. I'm leaving this bug open for that purpose. flutter gestures library



来源:https://stackoverflow.com/questions/51712287/how-to-disable-multi-touch-in-mobile-application-using-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!