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/services.dart';
class DraggableArea extends StatefulWidget {
final Widget child;
@@ -24,23 +25,29 @@ class _DraggableAreaState extends State<DraggableArea> {
double? first;
double? end;
FocusNode node = FocusNode();
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned(
left: left,
top: top,
child: Draggable(
feedback: widget.child,
childWhenDragging: const SizedBox.shrink(),
onDragUpdate: (u) => onUpdate(u),
onDragEnd: (detail) => onEnd(detail.offset),
child: widget.child,
return Focus(
focusNode: node,
autofocus: true,
onKeyEvent: onKeyInput,
child: Stack(
children: [
Positioned(
left: left,
top: top,
child: Draggable(
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;
end = null;
widget.moveChanged?.call(left, top);
node.requestFocus();
setState(() {});
}
onUpdate(DragUpdateDetails u) {
first ??= 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 offsetDx2 = 0;
double offsetDy2 = 0;
var windowFocusNode = FocusNode(canRequestFocus: false);
@override
Widget build(BuildContext context) {
@@ -83,7 +84,7 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
moveChanged: (dx, dy) {
offsetDx1 = dx;
offsetDy1 = dy;
setState(() {});
// setState(() {});
},
dx: offsetDx1,
dy: offsetDy1,
@@ -96,7 +97,8 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
moveChanged: (dx, dy) {
offsetDx2 = dx;
offsetDy2 = dy;
setState(() {});
windowFocusNode.unfocus();
// setState(() {});
},
dx: offsetDx2,
dy: offsetDy2,