gpt4 book ai didi

dart - 如何使用 flutter 在移动应用程序中禁用多点触控

转载 作者:IT老高 更新时间:2023-10-28 12:34:18 29 4
gpt4 key购买 nike

本题纯基于GestureDetectorflutter。

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

GestureDetector 引用链接:https://docs.flutter.io/flutter/widgets/GestureDetector-class.html

最佳答案

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

import 'package:flutter/material.dart' show
StatelessWidget, Widget, BuildContext, RawGestureDetector,
GestureRecognizerFactory, GestureRecognizerFactoryWithHandlers
;
import 'package:flutter/gestures.dart' show
OneSequenceGestureRecognizer, PointerDownEvent, GestureDisposition,
PointerEvent
;

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

实现示例:

OnlyOnePointerRecognizerWidget(
child: Text('test')
)

关于dart - 如何使用 flutter 在移动应用程序中禁用多点触控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51712287/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com