-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
map_drag_create.dart
203 lines (186 loc) · 6.44 KB
/
map_drag_create.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import 'dart:math' show Point;
import 'package:every_door/widgets/round_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart' show LatLng;
import 'package:logging/logging.dart';
class DragButton {
final IconData icon;
final Color? color;
final Function()? onDragStart;
final Function(LatLng)? onDragEnd;
final Function()? onTap;
final String? tooltip;
final Alignment alignment;
final EdgeInsets padding;
DragButton({
required this.icon,
this.color,
this.onDragStart,
this.onDragEnd,
this.onTap,
this.tooltip,
this.alignment = Alignment.bottomRight,
this.padding = EdgeInsets.zero,
});
}
class DragButtonWidget extends StatelessWidget {
final DragButton button;
final GlobalKey? mapKey;
static final _logger = Logger('DragButtonsWidget');
const DragButtonWidget({required this.button, this.mapKey});
@override
Widget build(BuildContext context) {
const arrowSize = 60.0;
final camera = MapCamera.of(context);
final safePadding =
MediaQuery.of(context).padding.copyWith(top: 0, bottom: 0);
const commonPadding =
EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0);
return Align(
alignment: button.alignment,
child: Padding(
padding: button.padding + safePadding + commonPadding,
child: Draggable(
data: button,
onDragStarted: () {
if (button.onDragStart != null) button.onDragStart!();
},
onDragEnd: (details) {
const offset = Point(-arrowSize / 2, -2.0);
final pos = Point(details.offset.dx, details.offset.dy);
// To adjust offset, we need to know the location of everything.
final mapOrigin =
mapKey?.currentContext!.findRenderObject()!.paintBounds.topLeft;
final globalMapOriginTr = mapKey?.currentContext!
.findRenderObject()!
.getTransformTo(null)
.getTranslation();
final globalMapOrigin = globalMapOriginTr == null
? Point(0.0, 0.0)
: Point(globalMapOriginTr.x, globalMapOriginTr.y);
_logger.info('Map origin: $mapOrigin, global: $globalMapOrigin, '
'drop offset: ${pos - offset}, padding: ${button.padding}.');
final location =
camera.pointToLatLng(pos - offset + globalMapOrigin);
if (button.onDragEnd != null) button.onDragEnd!(location);
},
feedbackOffset: Offset(arrowSize / 2, 70.0),
dragAnchorStrategy: (draggable, context, position) =>
Offset(arrowSize / 2, 70.0),
feedback: CustomPaint(
painter:
_ArrowUpPainter(button.color ?? Theme.of(context).primaryColor),
size: Size(arrowSize, 100.0),
),
childWhenDragging: Container(),
child: RoundButton(
icon: button.icon,
tooltip: button.tooltip,
onPressed: button.onTap,
),
),
),
);
}
}
class MapDragCreateButton extends StatelessWidget {
final IconData icon;
final Function()? onDragStart;
final Function(LatLng)? onDragEnd;
final Function()? onTap;
final String? tooltip;
final Alignment alignment;
final MapController map;
final GlobalKey? mapKey;
static const kArrowSize = 60.0;
static final _logger = Logger('MapDragCreateButton');
const MapDragCreateButton({
super.key,
this.mapKey,
required this.map,
required this.icon,
this.onDragStart,
this.onDragEnd,
this.onTap,
this.tooltip,
this.alignment = Alignment.bottomRight,
});
@override
Widget build(BuildContext context) {
final safePadding =
MediaQuery.of(context).padding.copyWith(top: 0, bottom: 0);
const commonPadding =
EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0);
return Align(
alignment: alignment,
child: Padding(
padding: safePadding + commonPadding,
child: Draggable(
onDragStarted: () {
if (onDragStart != null) onDragStart!();
},
onDragEnd: (details) {
const offset = Point(-kArrowSize / 2, -2.0);
final pos = Point(details.offset.dx, details.offset.dy);
// To adjust offset, we need to know the location of everything.
final globalMapOriginTr = mapKey?.currentContext!
.findRenderObject()!
.getTransformTo(null)
.getTranslation();
final globalMapOrigin = globalMapOriginTr == null
? Point(0.0, 0.0)
: Point(globalMapOriginTr.x, globalMapOriginTr.y);
_logger.info(
'global: $globalMapOrigin, drop offset: ${pos - offset}.');
final location =
map.camera.pointToLatLng(pos - offset + globalMapOrigin);
if (onDragEnd != null) onDragEnd!(location);
},
feedbackOffset: Offset(kArrowSize / 2, 70.0),
dragAnchorStrategy: (draggable, context, position) =>
Offset(kArrowSize / 2, 70.0),
feedback: CustomPaint(
painter: _ArrowUpPainter(Theme.of(context).primaryColor),
size: Size(kArrowSize, 100.0),
),
childWhenDragging: Container(),
child: RoundButton(
icon: icon,
tooltip: tooltip,
onPressed: onTap,
),
),
),
);
}
}
class _ArrowUpPainter extends CustomPainter {
final Color color;
_ArrowUpPainter(this.color);
@override
void paint(Canvas canvas, Size size) {
const kShiftDown = 8.0;
final paint = Paint()
..color = color
..strokeWidth = size.longestSide / 10.0
..strokeJoin = StrokeJoin.miter
..style = PaintingStyle.stroke;
final wingLevel = size.width / 2 + kShiftDown;
final path = Path()
..moveTo(size.width / 2, size.height / 30 + kShiftDown)
..lineTo(size.width / 2, size.height)
..moveTo(0, wingLevel)
..lineTo(size.width / 2, kShiftDown)
..lineTo(size.width, wingLevel);
canvas.drawPath(path, paint);
const kRedCircleRadius = 2.0;
final redPaint = Paint()
..color = Colors.red
..style = PaintingStyle.fill;
canvas.drawCircle(
Offset(size.width / 2, kRedCircleRadius), kRedCircleRadius, redPaint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}