一般来说,我对android开发非常陌生,如果其中一些是显而易见的答案,请原谅我。
我正在尝试实现向左滑动/向右滑动功能,以在应用程序的页面之间进行交换。但是,我遇到了检测速度并使用它来确定是否进行了左/右交换的问题。
这是我的代码,为清楚起见,保留了调试器说明:
onHorizontalDragStart: (DragStartDetails details) {
print("Horizontal Drag Start - preflop.");
},
onHorizontalDragDown: (DragDownDetails details) {
print("Horizontal Drag Down - preflop.");
},
onHorizontalDragUpdate: (DragUpdateDetails details) {
print("Horizontal Drag Update - preflop. Velocity Delta: " + details.primaryDelta.toString());
},
onHorizontalDragEnd: (DragEndDetails details) {
// TODO: Check velocity for non-zero but still too slow to swap?
if (details.primaryVelocity >= 0) {
print("Horizontal Drag End - preflop movement cancelled. Velocity: " + details.primaryVelocity.toString());
return;
}
else {
print("Horizontal Drag End - preflop. Velocity: " + details.primaryVelocity.toString());
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Flop()),
);
}
}
onHorizontalDragStart: (DragStartDetails details) {
print("Horizontal Drag Start - flop.");
},
onHorizontalDragDown: (DragDownDetails details) {
print("Horizontal Drag Down - flop.");
},
onHorizontalDragUpdate: (DragUpdateDetails details) {
print("Horizontal Drag Update - flop. Velocity Delta: " + details.primaryDelta.toString());
},
onHorizontalDragEnd: (DragEndDetails details) {
// TODO: Check velocity for non-zero but still too slow to swap?
if (details.primaryVelocity <= 0) {
print("Horizontal Drag End - flop movement cancelled. Velocity: " + details.primaryVelocity.toString());
return;
}
else {
print("Horizontal Drag End - flop. Velocity: " +
details.primaryVelocity.toString());
Navigator.pop(context);
}
}
当它正常工作时,这是我得到的调试输出:
I/flutter (17305): Horizontal Drag Down - preflop.
I/flutter (17305): Horizontal Drag Start - preflop.
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -5.712890625
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -5.336216517857167
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -7.998046875
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -8.763950892857167
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -11.42578125
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -11.049107142857167
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -11.049107142857167
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -13.334263392857139
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -11.42578125
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -8.763950892857139
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -7.998046875
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -5.336216517857139
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -3.427734375
I/flutter (17305): Horizontal Drag Update - preflop. Velocity Delta: -4.5703125
I/flutter (17305): Horizontal Drag End - preflop. Velocity: -710.2030737630879
我面临两个主要问题。首先,有时即使我正确地刷卡,primaryVelocity
仍返回零。可以通过跟踪onHorizontalDragUpdate
并查看生成的onHorizontalDragEnd
来见证这一点。现在,我了解到,如果我拖动然后停止然后放开光标,逻辑上结束速度将为零-但是在这种情况下,我没有感觉到暂停-只是拖动并释放。
I/flutter (17305): Horizontal Drag Down - flop.
I/flutter (17305): Horizontal Drag Start - flop.
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -6.85546875
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -8.763950892857167
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -7.998046875
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -9.906529017857167
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -9.140625
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -8.763950892857167
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -7.998046875
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -5.336216517857139
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -6.85546875
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -4.5703125
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -4.193638392857139
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -2.28515625
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -2.28515625
I/flutter (17305): Horizontal Drag End - flop movement cancelled. Velocity: 0.0
另一个问题是,有时primaryVelocity
的符号似乎不正确。例如,尽管onHorizontalDragUpdate
的值为primaryDelta
为负,但所得的onHorizontalDragUpdate
的速度为正值:
I/flutter (17305): Horizontal Drag Down - flop.
I/flutter (17305): Horizontal Drag Start - flop.
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -12.191685267857167
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -11.42578125
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -14.476841517857167
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -13.334263392857139
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -11.049107142857139
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -6.85546875
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -6.85546875
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -4.193638392857139
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -3.427734375
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -3.427734375
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -1.142578125
I/flutter (17305): Horizontal Drag Update - flop. Velocity Delta: -1.142578125
I/flutter (17305): Horizontal Drag End - flop. Velocity: 58.203062840330496
任何建议将不胜感激。让我知道我是否可以提供其他代码来帮助回答问题。谢谢!
编辑
根据Ulas的建议,我更新了所有插件/ IDE。似乎第一个问题已解决-我不再获得0.0的速度。
但是,我仍然遇到第二个问题:
I/flutter (18942): Horizontal Drag Down - flop.
I/flutter (18942): Horizontal Drag Start - flop.
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -6.478794642857167
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -7.998046875
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -8.763950892857167
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -9.140625
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -9.906529017857167
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -10.283203125
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -13.334263392857167
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -13.334263392857167
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -14.476841517857139
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -12.191685267857139
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -10.283203125
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -12.191685267857139
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -10.283203125
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -8.763950892857139
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -6.85546875
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -5.336216517857139
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -4.5703125
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -3.427734375
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -3.427734375
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -1.9084821428571388
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: -1.142578125
I/flutter (18942): Horizontal Drag Update - flop. Velocity Delta: 0.0
I/flutter (18942): Horizontal Drag End - flop. Velocity: 220.44058450541567
我的代码没有获得任何0.0的速度,您可以尝试其他波动通道,例如beta并升级最新版本吗?>