60 lines
1.3 KiB
Dart
60 lines
1.3 KiB
Dart
import 'package:flutter/material.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;
|
|
|
|
@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,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
void onEnd(Offset offset) {
|
|
left = offset.dx;
|
|
top = top + (end ?? 0) - (first ?? 0);
|
|
first = null;
|
|
end = null;
|
|
widget.moveChanged?.call(left, top);
|
|
}
|
|
|
|
onUpdate(DragUpdateDetails u) {
|
|
first ??= u.globalPosition.dy;
|
|
end = u.globalPosition.dy;
|
|
}
|
|
}
|