-
Notifications
You must be signed in to change notification settings - Fork 10
/
overlay_view.dart
75 lines (62 loc) · 1.92 KB
/
overlay_view.dart
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
73
74
75
import 'package:flutter/material.dart';
/// source: https://takeroro.github.io/2019/07/28/Flutter-Overlay/
class OverlayView {
static OverlayEntry _holder;
static Widget view;
static void remove() {
if (_holder != null) {
_holder.remove();
_holder = null;
}
}
static void show({@required BuildContext context, @required Widget view}) {
OverlayView.view = view;
remove();
OverlayEntry overlayEntry = new OverlayEntry(builder: (context) {
return new Positioned(top: MediaQuery.of(context).size.height * 0.7, right: 0, child: _buildDraggable(context));
});
Overlay.of(context).insert(overlayEntry);
_holder = overlayEntry;
}
static _buildDraggable(context) {
return new Draggable(
child: view,
feedback: view,
onDragStarted: () {},
onDragEnd: (detail) {
createDragTarget(offset: detail.offset, context: context);
},
childWhenDragging: Container(),
);
}
static void refresh() {
_holder.markNeedsBuild();
}
static void createDragTarget({Offset offset, BuildContext context}) {
if (_holder != null) {
_holder.remove();
}
_holder = new OverlayEntry(builder: (context) {
bool isLeft = true;
if (offset.dx + 100 > MediaQuery.of(context).size.width / 2) {
isLeft = false;
}
double maxY = MediaQuery.of(context).size.height - 100;
return new Positioned(
top: offset.dy < 50 ? 50 : offset.dy < maxY ? offset.dy : maxY,
left: isLeft ? 0 : null,
right: isLeft ? null : 0,
child: DragTarget(
onWillAccept: (data) {
return true;
},
onAccept: (data) {},
onLeave: (data) {},
builder: (BuildContext context, List incoming, List rejected) {
return _buildDraggable(context);
},
));
});
Overlay.of(context).insert(_holder);
}
}