作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
本题纯基于GestureDetector
flutter。
例如:在应用程序中,实现了 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/
我是一名优秀的程序员,十分优秀!