Repository navigation
Expand file tree
/
Copy pathdrag_example.dart
More file actions
72 lines (65 loc) · 1.85 KB
/
Copy pathdrag_example.dart
File metadata and controls
72 lines (65 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import 'package:flutter/material.dart';
import 'package:helgoboss_pointer_lock/helgoboss_pointer_lock.dart';
import 'package:helgoboss_pointer_lock_example/info_panel.dart';
class DragExample extends StatefulWidget {
final PointerLockCursor cursor;
final PointerLockWindowsMode windowsMode;
const DragExample({
super.key,
required this.cursor,
required this.windowsMode,
});
@override
State<DragExample> createState() => _DragExampleState();
}
class _DragExampleState extends State<DragExample> {
bool _dragging = false;
Offset _lastPointerDelta = Offset.zero;
Offset _firstPointerDelta = Offset.zero;
Offset _accumulation = Offset.zero;
@override
Widget build(BuildContext context) {
return PointerLockDragArea(
cursor: widget.cursor,
windowsMode: widget.windowsMode,
onLock: (_) => _setDragging(true),
onMove: (details) {
_setLastPointerDelta(details.move.delta);
},
onUnlock: (_) => _setDragging(false),
child: switch (_dragging) {
false => Center(
child: Text(
"Drag with left mouse button!",
style: Theme.of(context).textTheme.titleMedium,
),
),
true => InfoPanel(
firstPointerDelta: _firstPointerDelta,
lastPointerDelta: _lastPointerDelta,
accumulation: _accumulation,
),
},
);
}
void _setDragging(bool value) {
setState(() {
_firstPointerDelta = Offset.zero;
_lastPointerDelta = Offset.zero;
_accumulation = Offset.zero;
_dragging = value;
});
}
void _setLastPointerDelta(Offset delta) {
if (!mounted) {
return;
}
setState(() {
if (_firstPointerDelta == Offset.zero) {
_firstPointerDelta = delta;
}
_lastPointerDelta = delta;
_accumulation += delta;
});
}
}