feat: flutter优化滑动不跟手
This commit is contained in:
@@ -27,6 +27,7 @@ class RemoteTouchView extends StatefulWidget {
|
||||
) onSwipe;
|
||||
final void Function(int keyCode, int action) onKey;
|
||||
final void Function(double x, double y) onLongPress;
|
||||
final void Function(int action, double x, double y) onMotionEvent;
|
||||
|
||||
const RemoteTouchView({
|
||||
super.key,
|
||||
@@ -34,6 +35,7 @@ class RemoteTouchView extends StatefulWidget {
|
||||
required this.onSwipe,
|
||||
required this.onKey,
|
||||
required this.onLongPress,
|
||||
required this.onMotionEvent,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -77,9 +79,11 @@ class _RemoteTouchViewState extends State<RemoteTouchView> {
|
||||
// 捕获物理按键(外接键盘 / 遥控器等)。
|
||||
onKeyEvent: (node, event) {
|
||||
if (event is KeyDownEvent) {
|
||||
debugPrint('RemoteTouchView: KeyDown - ${event.logicalKey.debugName} (${event.logicalKey.keyId})');
|
||||
widget.onKey(event.logicalKey.keyId, 0);
|
||||
return KeyEventResult.handled;
|
||||
} else if (event is KeyUpEvent) {
|
||||
debugPrint('RemoteTouchView: KeyUp - ${event.logicalKey.debugName} (${event.logicalKey.keyId})');
|
||||
widget.onKey(event.logicalKey.keyId, 1);
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
@@ -93,17 +97,37 @@ class _RemoteTouchViewState extends State<RemoteTouchView> {
|
||||
_startPosition = event.localPosition;
|
||||
_currentPosition = event.localPosition;
|
||||
_startTime = DateTime.now();
|
||||
|
||||
final size = context.size;
|
||||
if (size != null) {
|
||||
final rx = _toRelativeX(event.localPosition, size);
|
||||
final ry = _toRelativeY(event.localPosition, size);
|
||||
debugPrint('RemoteTouchView: PointerDown - relX: ${rx.toStringAsFixed(3)}, relY: ${ry.toStringAsFixed(3)}');
|
||||
widget.onMotionEvent(0, rx, ry);
|
||||
}
|
||||
|
||||
_longPressTimer = Timer(_longPressTimeout, () {
|
||||
final size = context.size;
|
||||
if (_currentPosition == null || size == null) return;
|
||||
_isLongPressed = true;
|
||||
final x = _toRelativeX(_currentPosition!, size);
|
||||
final y = _toRelativeY(_currentPosition!, size);
|
||||
debugPrint('RemoteTouchView: LongPress - relX: ${x.toStringAsFixed(3)}, relY: ${y.toStringAsFixed(3)}');
|
||||
widget.onLongPress(x, y);
|
||||
});
|
||||
},
|
||||
onPointerMove: (event) {
|
||||
_currentPosition = event.localPosition;
|
||||
|
||||
final size = context.size;
|
||||
if (size != null) {
|
||||
final rx = _toRelativeX(event.localPosition, size);
|
||||
final ry = _toRelativeY(event.localPosition, size);
|
||||
// Move 事件较多,可以考虑控制日志频率,这里直接打印
|
||||
debugPrint('RemoteTouchView: PointerMove - relX: ${rx.toStringAsFixed(3)}, relY: ${ry.toStringAsFixed(3)}');
|
||||
widget.onMotionEvent(2, rx, ry);
|
||||
}
|
||||
|
||||
if (_longPressTimer != null && _startPosition != null) {
|
||||
final dx = (event.localPosition.dx - _startPosition!.dx).abs();
|
||||
final dy = (event.localPosition.dy - _startPosition!.dy).abs();
|
||||
@@ -114,6 +138,15 @@ class _RemoteTouchViewState extends State<RemoteTouchView> {
|
||||
},
|
||||
onPointerUp: (event) {
|
||||
_cancelLongPress();
|
||||
|
||||
final size = context.size;
|
||||
if (size != null) {
|
||||
final rx = _toRelativeX(event.localPosition, size);
|
||||
final ry = _toRelativeY(event.localPosition, size);
|
||||
debugPrint('RemoteTouchView: PointerUp - relX: ${rx.toStringAsFixed(3)}, relY: ${ry.toStringAsFixed(3)}');
|
||||
widget.onMotionEvent(1, rx, ry);
|
||||
}
|
||||
|
||||
// 长按已触发则不再派发单击 / 滑动事件(对应 Android 端 isLongPressed 逻辑)。
|
||||
if (_isLongPressed) {
|
||||
_startPosition = null;
|
||||
@@ -121,7 +154,6 @@ class _RemoteTouchViewState extends State<RemoteTouchView> {
|
||||
_startTime = null;
|
||||
return;
|
||||
}
|
||||
final size = context.size;
|
||||
if (_startPosition == null || size == null || _startTime == null) {
|
||||
return;
|
||||
}
|
||||
@@ -135,8 +167,10 @@ class _RemoteTouchViewState extends State<RemoteTouchView> {
|
||||
final dy = (endY - startY).abs();
|
||||
|
||||
if (duration < 200 && dx < 0.02 && dy < 0.02) {
|
||||
debugPrint('RemoteTouchView: Detected Touch - x: ${startX.toStringAsFixed(3)}, y: ${startY.toStringAsFixed(3)}');
|
||||
widget.onTouch(startX, startY);
|
||||
} else {
|
||||
debugPrint('RemoteTouchView: Detected Swipe - from (${startX.toStringAsFixed(3)}, ${startY.toStringAsFixed(3)}) to (${endX.toStringAsFixed(3)}, ${endY.toStringAsFixed(3)}), duration: ${max(duration, 1)}ms');
|
||||
widget.onSwipe(startX, startY, endX, endY, max(duration, 1));
|
||||
}
|
||||
_startPosition = null;
|
||||
|
||||
Reference in New Issue
Block a user