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

问题描述

这个问题纯粹基于GestureDetector颤动.

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

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

09-17 14:57