support use command and shift to fix the height or width

This commit is contained in:
Siyi Guo committed 2023-04-13 19:08:00 +08:00
1 parent c95c0e192e
commit cb4531f046
1 file changed
+53 -1
+53 -1
View File
@@ -27,6 +27,19 @@ class _DraggableAreaState extends State<DraggableArea> {
double? end;
FocusNode node = FocusNode();
bool isPressCommand = false;
bool isPressShift = false;
Axis? get axis {
if (isPressCommand) {
return Axis.vertical;
}
if (isPressShift) {
return Axis.horizontal;
}
return null;
}
@override
Widget build(BuildContext context) {
return Focus(
@@ -39,10 +52,15 @@ class _DraggableAreaState extends State<DraggableArea> {
left: left,
top: top,
child: Draggable(
axis: axis,
feedback: widget.child,
childWhenDragging: const SizedBox.shrink(),
onDragUpdate: (u) => onUpdate(u),
onDragEnd: (detail) => onEnd(detail.offset),
onDragStarted: () {
first = null;
end = null;
},
child: widget.child,
),
),
@@ -62,12 +80,31 @@ class _DraggableAreaState extends State<DraggableArea> {
}
onUpdate(DragUpdateDetails u) {
if (isPressShift) {
return;
}
first ??= u.globalPosition.dy;
end = u.globalPosition.dy;
}
KeyEventResult onKeyInput(FocusNode nodef, KeyEvent value) {
if (value is! KeyDownEvent && value is! KeyRepeatEvent) {
if (value is KeyDownEvent && isCommand(value)) {
isPressCommand = true;
setState(() {});
}
if (value is KeyUpEvent && isCommand(value)) {
isPressCommand = false;
setState(() {});
}
if (value is KeyDownEvent && isShift(value)) {
isPressShift = true;
setState(() {});
}
if (value is KeyUpEvent && isShift(value)) {
isPressShift = false;
setState(() {});
}
if (value is! KeyDownEvent && value is! KeyRepeatEvent) {
return KeyEventResult.ignored;
}
var x = 0;
@@ -90,3 +127,18 @@ class _DraggableAreaState extends State<DraggableArea> {
return KeyEventResult.handled;
}
}
bool isShift(KeyEvent e) {
var key = e.logicalKey;
return key == LogicalKeyboardKey.shiftLeft || key == LogicalKeyboardKey.shiftRight || key == LogicalKeyboardKey.shift;
}
bool isCommand(KeyEvent e) {
var key = e.logicalKey;
return key == LogicalKeyboardKey.metaLeft ||
key == LogicalKeyboardKey.metaRight ||
key == LogicalKeyboardKey.meta ||
key == LogicalKeyboardKey.controlLeft ||
key == LogicalKeyboardKey.controlRight ||
key == LogicalKeyboardKey.control;
}