如何让手势检测器检测Flutter Web中的两个手指拖动



我正试图在PageView中实现"平滑滚动",因此我将physics属性设置为NeverScrollableScrollPhysics(),并用检测拖动手势的GestureDetector包装构建器动作中的项目。当检测到拖动手势时,会触发一个功能,页面将相应地更改。

我的问题是,在Flutter Web上,只有当用三个手指拖动时才会检测到onVerticalDragGesture,而当用两个手指滑动时不会发生任何事情。有什么办法解决这个问题吗?

顺便说一句,结果与onPanUpdate属性相同。请随意建议其他实现平滑滚动的方法。提前谢谢,伙计们。

如果你想复制它,这是我的代码:

PageView.builder(
physics: NeverScrollableScrollPhysics(),
controller: /* PAGECONTROLLER */,
scrollDirection: Axis.vertical,
pageSnapping: false,
itemBuilder: (context, index) {
return GestureDetector(
onVerticalDragUpdate: (DragUpdateDetails details) {
if (details.delta.dy > 5.0) {
/* FUNCTION TO MOVE TO PREVIOUS PAGE */
}
if (details.delta.dy < -5.0) {
/* FUNCTION TO MOVE TO NEXT PAGE */
}
},
child: /* PAGES OF THE PAGEVIEW, JUST USE EMPTY CONTAINERS WITH DIFFERENT COLORS */,
);
},
itemCount: /* NUMBER OF PAGES */,
),

默认情况下,Flutter中的手势检测器只检测单个手指的手势。要检测多指手势,可以使用带有手势识别器的Listener小部件来识别多指手势。

下面的代码片段可能有助于使用Listener和PanGestureRecognizer:

Listener(
onPointerDown: (event) {
if (event.kind == PointerDeviceKind.touch && event.pointer != 0) {
// A second finger is down, start recognizing the gesture
_gestureRecognizer.addPointer(event);
}
},
onPointerUp: (event) {
if (event.kind == PointerDeviceKind.touch && event.pointer != 0) {
// The second finger is up, stop recognizing the gesture
_gestureRecognizer.removePointer(event);
}
},
child: Container(
// Your widget tree
),
);
// Initialize the gesture recognizer in your widget state
PanGestureRecognizer _gestureRecognizer = PanGestureRecognizer()
..onStart = (details) {
// This is called when the gesture starts
// Check that it is a two-finger gesture before handling it
if (_gestureRecognizer.numberOfPointers == 2) {
// Handle the two-finger drag gesture
print('Two-finger drag started');
}
}
..onUpdate = (details) {
// This is called when the gesture is updated
// Check that it is a two-finger gesture before handling it
if (_gestureRecognizer.numberOfPointers == 2) {
// Handle the two-finger drag gesture
print('Two-finger drag updated');
}
}
..onEnd = (details) {
// This is called when the gesture ends
// Check that it is a two-finger gesture before handling it
if (_gestureRecognizer.numberOfPointers == 2) {
// Handle the two-finger drag gesture
print('Two-finger drag ended');
}
};

相关内容

  • 没有找到相关文章

最新更新