-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathlocation_example.dart
More file actions
316 lines (287 loc) · 10.4 KB
/
Copy pathlocation_example.dart
File metadata and controls
316 lines (287 loc) · 10.4 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
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
307
308
309
310
311
312
313
314
315
316
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:geolocator/geolocator.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
import 'example.dart';
const _carModelUri = 'asset://assets/sportcar.glb';
const _colorSwatches = [
Colors.amber,
Colors.deepPurple,
Colors.cyan,
Colors.redAccent,
];
enum _PuckStyle {
defaultPuck('Default 2D'),
customIcon('Custom 2D'),
car('Custom 3D');
const _PuckStyle(this.label);
final String label;
}
class LocationExample extends StatefulWidget implements Example {
const LocationExample({super.key});
@override
final Widget leading = const Icon(Icons.map);
@override
final String title = 'Location Component';
@override
final String? subtitle = null;
@override
State<StatefulWidget> createState() => LocationExampleState();
}
class LocationExampleState extends State<LocationExample> {
MapboxMap? mapboxMap;
ViewportState? _viewport;
/// The single source of truth for every control below. Every mutation
/// goes through [_apply], which re-reads the merged settings from the
/// platform side right after writing, so the UI always reflects what's
/// actually active - not just what was last requested.
LocationComponentSettings? _settings;
Uint8List? _customIcon;
bool get _enabled => _settings?.enabled ?? false;
bool get _pulsingEnabled => _settings?.pulsingEnabled ?? false;
bool get _accuracyRingEnabled => _settings?.showAccuracyRing ?? false;
bool get _bearingEnabled => _settings?.puckBearingEnabled ?? false;
double get _modelScale {
final scale = _settings?.locationPuck?.locationPuck3D?.modelScale;
if (scale == null || scale.isEmpty) return 8.0;
return scale.first ?? 8.0;
}
/// Tracked explicitly rather than read back from the platform: the map
/// always starts on the default puck, and every other value is assigned
/// by [_selectPuckStyle] itself, since it already knows which style it's
/// requesting.
_PuckStyle _puckStyle = _PuckStyle.defaultPuck;
bool get _is3DPuckActive => _puckStyle == _PuckStyle.car;
@override
void initState() {
super.initState();
rootBundle.load('assets/symbols/custom-icon.png').then((bytes) {
_customIcon = bytes.buffer.asUint8List();
});
}
void _onMapCreated(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.location.getSettings().then((settings) {
if (!mounted) return;
setState(() => _settings = settings);
});
}
/// Applies a partial [LocationComponentSettings] update, then re-reads the
/// full settings back from the platform so [_settings] stays in sync with
/// what's actually configured. Pass [style] when the caller already knows
/// which puck style this update selects, so [_puckStyle] can be assigned
/// directly.
Future<void> _apply(LocationComponentSettings settings,
{_PuckStyle? style}) async {
final location = mapboxMap?.location;
if (location == null) return;
await location.updateSettings(settings);
final updated = await location.getSettings();
if (!mounted) return;
setState(() {
_settings = updated;
if (style != null) _puckStyle = style;
});
}
Future<void> _toggleLocation() async {
final next = !_enabled;
if (next) {
await Geolocator.requestPermission();
}
await _apply(LocationComponentSettings(enabled: next));
if (!mounted) return;
setStateWithViewportAnimation(() {
_viewport =
next ? const FollowPuckViewportState() : const IdleViewportState();
});
}
Future<void> _selectPuckStyle(_PuckStyle style) {
switch (style) {
case _PuckStyle.defaultPuck:
return _apply(
LocationComponentSettings(
locationPuck: LocationPuck(locationPuck2D: DefaultLocationPuck2D()),
),
style: style,
);
case _PuckStyle.customIcon:
return _apply(
LocationComponentSettings(
locationPuck: LocationPuck(
locationPuck2D: DefaultLocationPuck2D(topImage: _customIcon),
),
),
style: style,
);
case _PuckStyle.car:
return _apply(
LocationComponentSettings(
locationPuck: LocationPuck(
locationPuck3D: LocationPuck3D(
modelUri: _carModelUri,
modelScale: [_modelScale, _modelScale, _modelScale],
),
),
),
style: style,
);
}
}
Future<void> _setModelScale(double scale) {
// A partial 3D update: modelUri is intentionally left out, so the
// currently active model is kept and only its scale changes.
return _apply(LocationComponentSettings(
locationPuck: LocationPuck(
locationPuck3D: LocationPuck3D(modelScale: [scale, scale, scale]),
),
));
}
Future<void> _setPulsing(bool value) =>
_apply(LocationComponentSettings(pulsingEnabled: value));
Future<void> _setAccuracyRing(bool value) =>
_apply(LocationComponentSettings(showAccuracyRing: value));
Future<void> _setBearing(bool value) =>
_apply(LocationComponentSettings(puckBearingEnabled: value));
Future<void> _setPulsingColor(Color color) =>
_apply(LocationComponentSettings(pulsingColor: color.value));
Future<void> _setAccuracyRingColor(Color color) =>
_apply(LocationComponentSettings(accuracyRingColor: color.value));
Widget _colorSwatchRow({
required int? selectedColor,
required ValueChanged<Color> onSelect,
}) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Row(
children: [
for (final color in _colorSwatches)
Padding(
padding: const EdgeInsets.only(right: 8),
child: InkWell(
borderRadius: BorderRadius.circular(16),
onTap: () => onSelect(color),
child: CircleAvatar(
radius: 14,
backgroundColor: color,
child: selectedColor == color.value
? const Icon(Icons.check, size: 16, color: Colors.white)
: null,
),
),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: MapWidget(
key: const ValueKey('mapWidget'),
onMapCreated: _onMapCreated,
viewport: _viewport,
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _toggleLocation,
icon: Icon(_enabled ? Icons.location_disabled : Icons.my_location),
label: Text(_enabled ? 'Hide puck' : 'Show puck'),
),
bottomNavigationBar: Card(
margin: EdgeInsets.zero,
elevation: 8,
child: SafeArea(
top: false,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: SegmentedButton<_PuckStyle>(
segments: [
for (final style in _PuckStyle.values)
ButtonSegment(
value: style,
label: Text(style.label,
style: const TextStyle(fontSize: 12)),
),
],
selected: {
_puckStyle
},
onSelectionChanged: (selection) =>
_selectPuckStyle(selection.first)),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
Row(
children: [
const Icon(Icons.photo_size_select_small, size: 20),
Expanded(
child: Slider(
min: 1,
max: 20,
value: _modelScale.clamp(1.0, 20.0).toDouble(),
label: _modelScale.toStringAsFixed(1),
onChanged:
_is3DPuckActive ? _setModelScale : null,
),
),
],
),
if (!_is3DPuckActive)
const Padding(
padding: EdgeInsets.only(left: 40),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
'3D puck only',
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
),
),
],
),
),
SwitchListTile(
title: const Text('Pulsing'),
subtitle: _is3DPuckActive ? const Text('2D puck only') : null,
value: _pulsingEnabled,
onChanged: !_is3DPuckActive ? _setPulsing : null,
),
if (_pulsingEnabled && !_is3DPuckActive)
_colorSwatchRow(
selectedColor: _settings?.pulsingColor,
onSelect: _setPulsingColor,
),
SwitchListTile(
title: const Text('Accuracy ring'),
subtitle: _is3DPuckActive ? const Text('2D puck only') : null,
value: _accuracyRingEnabled,
onChanged: !_is3DPuckActive ? _setAccuracyRing : null,
),
if (_accuracyRingEnabled && !_is3DPuckActive)
_colorSwatchRow(
selectedColor: _settings?.accuracyRingColor,
onSelect: _setAccuracyRingColor,
),
SwitchListTile(
title: const Text('Bearing'),
value: _bearingEnabled,
onChanged: _setBearing,
),
],
),
),
),
),
);
}
}