From cb4531f0469c08ec3d06471cb00540ce13e41802 Mon Sep 17 00:00:00 2001 From: Siyi Guo Date: Thu, 13 Apr 2023 19:08:00 +0800 Subject: [PATCH] support use command and shift to fix the height or width --- lib/image/drag.dart | 54 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/image/drag.dart b/lib/image/drag.dart index cb364aa..75eb55b 100644 --- a/lib/image/drag.dart +++ b/lib/image/drag.dart @@ -27,6 +27,19 @@ class _DraggableAreaState extends State { 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 { 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 { } 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 { 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; +}