-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.dart
306 lines (279 loc) · 9.51 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_animations/flutter_map_animations.dart';
import 'package:flutter_map_cancellable_tile_provider/flutter_map_cancellable_tile_provider.dart';
import 'package:latlong2/latlong.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
static const _useTransformerId = 'useTransformerId';
final markers = ValueNotifier<List<AnimatedMarker>>([]);
final center = const LatLng(51.509364, -0.128928);
bool _useTransformer = true;
int _lastMovedToMarkerIndex = -1;
late final _animatedMapController = AnimatedMapController(vsync: this);
@override
void dispose() {
markers.dispose();
_animatedMapController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ValueListenableBuilder<List<AnimatedMarker>>(
valueListenable: markers,
builder: (context, markers, _) {
return FlutterMap(
mapController: _animatedMapController.mapController,
options: MapOptions(
initialCenter: center,
onTap: (_, point) => _addMarker(point),
),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'com.example.app',
tileUpdateTransformer: _animatedMoveTileUpdateTransformer,
tileProvider: CancellableNetworkTileProvider(),
),
AnimatedMarkerLayer(markers: markers),
],
);
},
),
floatingActionButton: SeparatedColumn(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.end,
separator: const SizedBox(height: 8),
children: [
FloatingActionButton(
onPressed: () => _animatedMapController.animatedRotateFrom(
90,
customId: _useTransformer ? _useTransformerId : null,
),
tooltip: 'Rotate 90°',
child: const Icon(Icons.rotate_right),
),
FloatingActionButton(
onPressed: () => _animatedMapController.animatedRotateFrom(
-90,
customId: _useTransformer ? _useTransformerId : null,
),
tooltip: 'Rotate -90°',
child: const Icon(Icons.rotate_left),
),
FloatingActionButton(
onPressed: () {
markers.value = [];
_animatedMapController.animateTo(
dest: center,
rotation: 0,
customId: _useTransformer ? _useTransformerId : null,
);
},
tooltip: 'Clear modifications',
child: const Icon(Icons.clear_all),
),
FloatingActionButton(
onPressed: () => _animatedMapController.animatedZoomIn(
customId: _useTransformer ? _useTransformerId : null,
),
tooltip: 'Zoom in',
child: const Icon(Icons.zoom_in),
),
FloatingActionButton(
onPressed: () => _animatedMapController.animatedZoomOut(
customId: _useTransformer ? _useTransformerId : null,
),
tooltip: 'Zoom out',
child: const Icon(Icons.zoom_out),
),
FloatingActionButton(
tooltip: 'Center on markers',
onPressed: () {
if (markers.value.length < 2) return;
final points = markers.value.map((m) => m.point).toList();
_animatedMapController.animatedFitCamera(
cameraFit: CameraFit.coordinates(
coordinates: points,
padding: const EdgeInsets.all(12),
),
rotation: 0,
customId: _useTransformer ? _useTransformerId : null,
);
},
child: const Icon(Icons.center_focus_strong),
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
FloatingActionButton(
tooltip: 'Move to next marker with offset',
onPressed: () {
if (markers.value.isEmpty) return;
final points = markers.value.map((m) => m.point);
setState(
() => _lastMovedToMarkerIndex =
(_lastMovedToMarkerIndex + 1) % points.length,
);
_animatedMapController.animateTo(
dest: points.elementAt(_lastMovedToMarkerIndex),
customId: _useTransformer ? _useTransformerId : null,
offset: const Offset(100, 100),
);
},
child: const Icon(Icons.multiple_stop),
),
const SizedBox.square(dimension: 8),
FloatingActionButton(
tooltip: 'Move to next marker',
onPressed: () {
if (markers.value.isEmpty) return;
final points = markers.value.map((m) => m.point);
setState(
() => _lastMovedToMarkerIndex =
(_lastMovedToMarkerIndex + 1) % points.length,
);
_animatedMapController.animateTo(
dest: points.elementAt(_lastMovedToMarkerIndex),
customId: _useTransformer ? _useTransformerId : null,
);
},
child: const Icon(Icons.polyline_rounded),
),
],
),
FloatingActionButton.extended(
label: Row(
children: [
const Text('Transformer'),
Switch(
activeColor: Colors.blue.shade200,
activeTrackColor: Colors.black38,
value: _useTransformer,
onChanged: (newValue) {
setState(() => _useTransformer = newValue);
},
),
],
),
onPressed: () {
setState(() => _useTransformer = !_useTransformer);
},
),
],
),
);
}
void _addMarker(LatLng point) {
markers.value = List.from(markers.value)
..add(
MyMarker(
point: point,
onTap: () => _animatedMapController.animateTo(
dest: point,
customId: _useTransformer ? _useTransformerId : null,
),
),
);
}
}
class MyMarker extends AnimatedMarker {
MyMarker({
required super.point,
VoidCallback? onTap,
}) : super(
width: markerSize,
height: markerSize,
builder: (context, animation) {
final size = markerSize * animation.value;
return GestureDetector(
onTap: onTap,
child: Opacity(
opacity: animation.value,
child: Icon(
Icons.room,
size: size,
),
),
);
},
);
static const markerSize = 50.0;
}
class SeparatedColumn extends StatelessWidget {
const SeparatedColumn({
super.key,
required this.separator,
this.children = const [],
this.mainAxisSize = MainAxisSize.max,
this.crossAxisAlignment = CrossAxisAlignment.start,
});
final Widget separator;
final List<Widget> children;
final MainAxisSize mainAxisSize;
final CrossAxisAlignment crossAxisAlignment;
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: mainAxisSize,
crossAxisAlignment: crossAxisAlignment,
children: [
..._buildChildren(),
],
);
}
Iterable<Widget> _buildChildren() sync* {
for (var i = 0; i < children.length; i++) {
yield children[i];
if (i < children.length - 1) yield separator;
}
}
}
/// Inspired by the contribution of [rorystephenson](https://github.com/fleaflet/flutter_map/pull/1475/files#diff-b663bf9f32e20dbe004bd1b58a53408aa4d0c28bcc29940156beb3f34e364556)
final _animatedMoveTileUpdateTransformer = TileUpdateTransformer.fromHandlers(
handleData: (updateEvent, sink) {
final id = AnimationId.fromMapEvent(updateEvent.mapEvent);
if (id == null) return sink.add(updateEvent);
if (id.customId != _MyHomePageState._useTransformerId) {
if (id.moveId == AnimatedMoveId.started) {
debugPrint('TileUpdateTransformer disabled, using default behaviour.');
}
return sink.add(updateEvent);
}
switch (id.moveId) {
case AnimatedMoveId.started:
debugPrint('Loading tiles at animation destination.');
sink.add(
updateEvent.loadOnly(
loadCenterOverride: id.destLocation,
loadZoomOverride: id.destZoom,
),
);
break;
case AnimatedMoveId.inProgress:
// Do not prune or load during movement.
break;
case AnimatedMoveId.finished:
debugPrint('Pruning tiles after animated movement.');
sink.add(updateEvent.pruneOnly());
break;
}
},
);