add the keyboard control

This commit is contained in:
Siyi Guo committed 2023-04-13 14:04:40 +08:00
1 parent dea0bccedf
commit 6faf8ed0bb
2 files changed
+50 -15

No files matched your search

+46 -13
View File
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class DraggableArea extends StatefulWidget { class DraggableArea extends StatefulWidget {
final Widget child; final Widget child;
@@ -24,23 +25,29 @@ class _DraggableAreaState extends State<DraggableArea> {
double? first; double? first;
double? end; double? end;
FocusNode node = FocusNode();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Stack( return Focus(
children: [ focusNode: node,
Positioned( autofocus: true,
left: left, onKeyEvent: onKeyInput,
top: top, child: Stack(
child: Draggable( children: [
feedback: widget.child, Positioned(
childWhenDragging: const SizedBox.shrink(), left: left,
onDragUpdate: (u) => onUpdate(u), top: top,
onDragEnd: (detail) => onEnd(detail.offset), child: Draggable(
child: widget.child, feedback: widget.child,
childWhenDragging: const SizedBox.shrink(),
onDragUpdate: (u) => onUpdate(u),
onDragEnd: (detail) => onEnd(detail.offset),
child: widget.child,
),
), ),
), ],
], ),
); );
} }
@@ -50,10 +57,36 @@ class _DraggableAreaState extends State<DraggableArea> {
first = null; first = null;
end = null; end = null;
widget.moveChanged?.call(left, top); widget.moveChanged?.call(left, top);
node.requestFocus();
setState(() {});
} }
onUpdate(DragUpdateDetails u) { onUpdate(DragUpdateDetails u) {
first ??= u.globalPosition.dy; first ??= u.globalPosition.dy;
end = u.globalPosition.dy; end = u.globalPosition.dy;
} }
KeyEventResult onKeyInput(FocusNode nodef, KeyEvent value) {
if (value is! KeyDownEvent && value is! KeyRepeatEvent) {
return KeyEventResult.ignored;
}
var x = 0;
var y = 0;
if (value.logicalKey == LogicalKeyboardKey.arrowDown || value.logicalKey == LogicalKeyboardKey.keyJ) {
y = 1;
} else if (value.logicalKey == LogicalKeyboardKey.arrowUp || value.logicalKey == LogicalKeyboardKey.keyK) {
y = -1;
} else if (value.logicalKey == LogicalKeyboardKey.arrowRight || value.logicalKey == LogicalKeyboardKey.keyL) {
x = 1;
} else if (value.logicalKey == LogicalKeyboardKey.arrowLeft || value.logicalKey == LogicalKeyboardKey.keyH) {
x = -1;
}
if (x != 0 || y != 0) {
left += x;
top += y;
widget.moveChanged?.call(left, top);
setState(() {});
}
return KeyEventResult.handled;
}
} }
+4 -2
View File
@@ -28,6 +28,7 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
double offsetDy1 = 0; double offsetDy1 = 0;
double offsetDx2 = 0; double offsetDx2 = 0;
double offsetDy2 = 0; double offsetDy2 = 0;
var windowFocusNode = FocusNode(canRequestFocus: false);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -83,7 +84,7 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
moveChanged: (dx, dy) { moveChanged: (dx, dy) {
offsetDx1 = dx; offsetDx1 = dx;
offsetDy1 = dy; offsetDy1 = dy;
setState(() {}); // setState(() {});
}, },
dx: offsetDx1, dx: offsetDx1,
dy: offsetDy1, dy: offsetDy1,
@@ -96,7 +97,8 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
moveChanged: (dx, dy) { moveChanged: (dx, dy) {
offsetDx2 = dx; offsetDx2 = dx;
offsetDy2 = dy; offsetDy2 = dy;
setState(() {}); windowFocusNode.unfocus();
// setState(() {});
}, },
dx: offsetDx2, dx: offsetDx2,
dy: offsetDy2, dy: offsetDy2,