Flutter GestureDetector 上滑切换问题解决方法
这段代码中使用了 GestureDetector,在用户水平拖动结束时切换内容。然而,由于手指在屏幕上滑动时也会产生一定的水平移动,因此在上下滑动时也可能会触发切换。为避免这种情况,可以添加一个判断条件,判断手指移动的距离是否大于一个阈值,只有当手指移动的距离超过该阈值时才执行切换操作。可以使用 onVerticalDragUpdate 回调函数来获取手指在垂直方向上的移动距离,并在 onHorizontalDragEnd 回调函数中添加判断条件。例如,只有当手指在水平方向上移动的距离大于手指在垂直方向上移动的距离时才执行切换操作。
GestureDetector(
onHorizontalDragEnd: (details) {
if (details.velocity.pixelsPerSecond.dx < 0 && details.primaryDelta.abs() < details.delta.dx.abs()) { // 添加判断条件
hItems(Global.calendar_current_day.add(Duration(days: 1)));
_calendarAgendaControllerAppBar.goToDay(Global.calendar_current_day);
} else if (details.velocity.pixelsPerSecond.dx > 0 && details.primaryDelta.abs() < details.delta.dx.abs()) { // 添加判断条件
hItems(Global.calendar_current_day.subtract(Duration(days: 1)));
_calendarAgendaControllerAppBar.goToDay(Global.calendar_current_day); //
}
},
onVerticalDragUpdate: (details) { // 获取垂直方向移动距离
// ...
},
child: SideMenu(
// ...
),
);
通过这种方式,可以有效避免上下滑动时误触切换操作。
原文地址: https://www.cveoy.top/t/topic/obTa 著作权归作者所有。请勿转载和采集!