add the keyboard control
This commit is contained in:
1 parent
dea0bccedf
commit
6faf8ed0bb
2 files changed
+38
-3
No files matched your search
+34
-1
@@ -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,10 +25,15 @@ 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(
|
||||||
|
focusNode: node,
|
||||||
|
autofocus: true,
|
||||||
|
onKeyEvent: onKeyInput,
|
||||||
|
child: Stack(
|
||||||
children: [
|
children: [
|
||||||
Positioned(
|
Positioned(
|
||||||
left: left,
|
left: left,
|
||||||
@@ -41,6 +47,7 @@ class _DraggableAreaState extends State<DraggableArea> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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,
|
||||||
|
|||||||
Reference in new issue
Block a user