本文介绍了如何使用颤振在移动应用程序中禁用多点触控的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题纯粹是基于GestureDetectorflutter.

This question is purely based on GestureDetector flutter.

例如:在应用程序中,实现了 GestureDetector 类,因此默认情况下它支持多点触控,现在需要禁用此多点触控,以便最好的解决方案.

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 参考链接:https://docs.flutter.io/flutter/widgets/GestureDetector-class.html

推荐答案

创建 OnlyOnePointerRecognizerWidget 小部件的实例并将任何小部件作为子部件传递给它.OnlyOnePointerRecognizerWidget 只会识别一个指针.

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,
    );
  }
}

这篇关于如何使用颤振在移动应用程序中禁用多点触控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 16:48