148 lines
3.8 KiB
Dart
148 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class DraggableArea extends StatefulWidget {
|
|
final Widget child;
|
|
|
|
const DraggableArea({
|
|
Key? key,
|
|
required this.child,
|
|
this.moveChanged,
|
|
this.dx = 0,
|
|
this.dy = 0,
|
|
}) : super(key: key);
|
|
final Function(double dx, double dy)? moveChanged;
|
|
final double dx;
|
|
final double dy;
|
|
|
|
@override
|
|
State<DraggableArea> createState() => _DraggableAreaState();
|
|
}
|
|
|
|
class _DraggableAreaState extends State<DraggableArea> {
|
|
late double left = widget.dx;
|
|
late double top = widget.dy;
|
|
|
|
double? first;
|
|
double? end;
|
|
FocusNode node = FocusNode();
|
|
|
|
bool isPressCommand = false;
|
|
bool isPressShift = false;
|
|
|
|
Axis? get axis {
|
|
if (isPressShift) {
|
|
return Axis.horizontal;
|
|
}
|
|
if (isPressCommand) {
|
|
return Axis.vertical;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Focus(
|
|
focusNode: node,
|
|
autofocus: true,
|
|
onKeyEvent: onKeyInput,
|
|
child: Stack(
|
|
children: [
|
|
Positioned(
|
|
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,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void onEnd(Offset offset) {
|
|
left = offset.dx;
|
|
top = top + (end ?? 0) - (first ?? 0);
|
|
first = null;
|
|
end = null;
|
|
widget.moveChanged?.call(left, top);
|
|
node.requestFocus();
|
|
setState(() {});
|
|
}
|
|
|
|
onUpdate(DragUpdateDetails u) {
|
|
if (isPressShift) {
|
|
return;
|
|
}
|
|
first ??= u.globalPosition.dy;
|
|
end = u.globalPosition.dy;
|
|
}
|
|
|
|
KeyEventResult onKeyInput(FocusNode nodef, KeyEvent value) {
|
|
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)) {
|
|
Future.delayed(const Duration(milliseconds: 500)).then((value) {
|
|
isPressShift = false;
|
|
if (!mounted) return;
|
|
setState(() {});
|
|
});
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|