Skip to content

Commit e2ff90d

Browse files
feat: add ability to use meters to size Markers (#2106)
Co-authored-by: Luka S <github@jaffaketchup.dev>
1 parent 850b918 commit e2ff90d

5 files changed

Lines changed: 562 additions & 181 deletions

File tree

‎example/lib/pages/many_circles.dart‎

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ class _ManyCirclesPageState extends State<ManyCirclesPage> {
9999
right: 16,
100100
child: RepaintBoundary(
101101
child: Column(
102+
spacing: 12,
102103
children: [
103104
NumberOfItemsSlider(
104105
number: displayedCirclesCount,
105106
onChanged: (v) => setState(() => displayedCirclesCount = v),
106107
maxNumber: _maxCirclesCount,
107108
itemDescription: 'Circle',
108109
),
109-
const SizedBox(height: 12),
110110
UnconstrainedBox(
111111
child: Container(
112112
decoration: BoxDecoration(
@@ -117,71 +117,70 @@ class _ManyCirclesPageState extends State<ManyCirclesPage> {
117117
vertical: 4,
118118
horizontal: 16,
119119
),
120-
child: Row(
121-
children: [
122-
const Tooltip(
123-
message: 'Use Borders',
124-
child: Icon(Icons.circle_outlined),
125-
),
126-
const SizedBox(width: 8),
127-
Switch.adaptive(
128-
value: useBorders,
129-
onChanged: (v) {
130-
allCircles = allCircles
131-
.map(
132-
(c) => CircleMarker(
133-
point: c.point,
134-
radius: c.radius,
135-
color: c.color,
136-
useRadiusInMeter: c.useRadiusInMeter,
137-
borderColor: c.borderColor,
138-
borderStrokeWidth: v ? 5 : 0,
139-
),
140-
)
141-
.toList(growable: false);
142-
useBorders = v;
143-
setState(() {});
144-
},
145-
),
146-
const SizedBox(width: 16),
147-
const Tooltip(
148-
message: 'Use Radius In Meters',
149-
child: Icon(Icons.straighten),
150-
),
151-
const SizedBox(width: 8),
152-
Switch.adaptive(
153-
value: useRadiusInMeters,
154-
onChanged: (v) {
155-
allCircles = allCircles
156-
.map(
157-
(c) => CircleMarker(
158-
point: c.point,
159-
radius: v ? 25000 : 5,
160-
color: c.color,
161-
useRadiusInMeter: v,
162-
borderColor: c.borderColor,
163-
borderStrokeWidth: c.borderStrokeWidth,
164-
),
165-
)
166-
.toList(growable: false);
167-
useRadiusInMeters = v;
168-
setState(() {});
169-
},
170-
),
171-
const SizedBox(width: 16),
172-
const Tooltip(
173-
message: 'Optimise Meters Radius',
174-
child: Icon(Icons.speed_rounded),
175-
),
176-
const SizedBox(width: 8),
177-
Switch.adaptive(
178-
value: optimizeRadiusInMeters,
179-
onChanged: useRadiusInMeters
180-
? (v) =>
181-
setState(() => optimizeRadiusInMeters = v)
182-
: null,
183-
),
184-
],
120+
child: IntrinsicHeight(
121+
child: Row(
122+
spacing: 8,
123+
children: [
124+
const Tooltip(
125+
message: 'Use Borders',
126+
child: Icon(Icons.circle_outlined),
127+
),
128+
Switch.adaptive(
129+
value: useBorders,
130+
onChanged: (v) {
131+
allCircles = allCircles
132+
.map(
133+
(c) => CircleMarker(
134+
point: c.point,
135+
radius: c.radius,
136+
color: c.color,
137+
useRadiusInMeter: c.useRadiusInMeter,
138+
borderColor: c.borderColor,
139+
borderStrokeWidth: v ? 5 : 0,
140+
),
141+
)
142+
.toList(growable: false);
143+
useBorders = v;
144+
setState(() {});
145+
},
146+
),
147+
const VerticalDivider(),
148+
const Tooltip(
149+
message: 'Use Radius In Meters',
150+
child: Icon(Icons.straighten),
151+
),
152+
Switch.adaptive(
153+
value: useRadiusInMeters,
154+
onChanged: (v) {
155+
allCircles = allCircles
156+
.map(
157+
(c) => CircleMarker(
158+
point: c.point,
159+
radius: v ? 25000 : 5,
160+
color: c.color,
161+
useRadiusInMeter: v,
162+
borderColor: c.borderColor,
163+
borderStrokeWidth: c.borderStrokeWidth,
164+
),
165+
)
166+
.toList(growable: false);
167+
useRadiusInMeters = v;
168+
setState(() {});
169+
},
170+
),
171+
const Tooltip(
172+
message: 'Optimise Meters Radius',
173+
child: Icon(Icons.speed_rounded),
174+
),
175+
Switch.adaptive(
176+
value: optimizeRadiusInMeters,
177+
onChanged: useRadiusInMeters
178+
? (v) =>
179+
setState(() => optimizeRadiusInMeters = v)
180+
: null,
181+
),
182+
],
183+
),
185184
),
186185
),
187186
),

‎example/lib/pages/many_markers.dart‎

Lines changed: 131 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ManyMarkersPage extends StatefulWidget {
3030

3131
class ManyMarkersPageState extends State<ManyMarkersPage> {
3232
final randomGenerator = Random(10);
33-
late final allMarkers = List.generate(
33+
late List<Marker> allMarkers = List.generate(
3434
_maxMarkersCount,
3535
(_) {
3636
final angle = randomGenerator.nextDouble() * 2 * pi;
@@ -46,35 +46,25 @@ class ManyMarkersPageState extends State<ManyMarkersPage> {
4646

4747
return Marker(
4848
point: position,
49-
width: 30,
50-
height: 30,
51-
child: GestureDetector(
52-
onTap: () => ScaffoldMessenger.of(context).showSnackBar(
53-
SnackBar(
54-
content: Text(
55-
'Tapped existing marker (${position.latitude}, '
56-
'${position.longitude})',
57-
),
58-
duration: const Duration(seconds: 1),
59-
showCloseIcon: true,
60-
),
61-
),
62-
child: Icon(
63-
Icons.location_pin,
64-
size: 30,
65-
color: Color.fromARGB(
66-
255,
67-
randomGenerator.nextInt(256),
68-
randomGenerator.nextInt(256),
69-
randomGenerator.nextInt(256),
70-
),
49+
child: Icon(
50+
Icons.location_pin,
51+
size: 30,
52+
color: Color.fromARGB(
53+
255,
54+
randomGenerator.nextInt(256),
55+
randomGenerator.nextInt(256),
56+
randomGenerator.nextInt(256),
7157
),
7258
),
7359
);
7460
},
7561
);
7662
int displayedMarkersCount = _maxMarkersCount ~/ 10;
7763

64+
bool useIcons = true;
65+
bool useSizeInMeters = false;
66+
bool optimizeSizeInMeters = true;
67+
7868
@override
7969
void initState() {
8070
super.initState();
@@ -103,18 +93,72 @@ class ManyMarkersPageState extends State<ManyMarkersPage> {
10393
markers: allMarkers
10494
.take(displayedMarkersCount)
10595
.toList(growable: false),
96+
optimizeDimensionsInMeters: optimizeSizeInMeters,
10697
),
10798
],
10899
),
109100
Positioned(
110101
left: 16,
111102
top: 16,
112103
right: 16,
113-
child: NumberOfItemsSlider(
114-
number: displayedMarkersCount,
115-
onChanged: (v) => setState(() => displayedMarkersCount = v),
116-
maxNumber: _maxMarkersCount,
117-
itemDescription: 'Marker',
104+
child: Column(
105+
spacing: 12,
106+
children: [
107+
NumberOfItemsSlider(
108+
number: displayedMarkersCount,
109+
onChanged: (v) => setState(() => displayedMarkersCount = v),
110+
maxNumber: _maxMarkersCount,
111+
itemDescription: 'Marker',
112+
),
113+
UnconstrainedBox(
114+
child: Container(
115+
decoration: BoxDecoration(
116+
color: Theme.of(context).colorScheme.surface,
117+
borderRadius: BorderRadius.circular(32),
118+
),
119+
padding: const EdgeInsets.symmetric(
120+
vertical: 4,
121+
horizontal: 16,
122+
),
123+
child: IntrinsicHeight(
124+
child: Row(
125+
spacing: 8,
126+
children: [
127+
const Tooltip(
128+
message: 'Use Icons',
129+
child: Icon(Icons.location_on),
130+
),
131+
Switch.adaptive(
132+
value: useIcons,
133+
onChanged: _useIconsToggler,
134+
),
135+
const VerticalDivider(),
136+
const Tooltip(
137+
message: 'Use Radius In Meters',
138+
child: Icon(Icons.straighten),
139+
),
140+
Switch.adaptive(
141+
value: useSizeInMeters,
142+
onChanged:
143+
useIcons ? null : _useDimensionsInMetersToggler,
144+
),
145+
const Tooltip(
146+
message: 'Optimise Meters Radius',
147+
child: Icon(Icons.speed_rounded),
148+
),
149+
Switch.adaptive(
150+
value: optimizeSizeInMeters,
151+
onChanged: useSizeInMeters && !useIcons
152+
? (v) =>
153+
setState(() => optimizeSizeInMeters = v)
154+
: null,
155+
),
156+
],
157+
),
158+
),
159+
),
160+
),
161+
],
118162
),
119163
),
120164
if (!kIsWeb)
@@ -128,4 +172,63 @@ class ManyMarkersPageState extends State<ManyMarkersPage> {
128172
),
129173
);
130174
}
175+
176+
void _useDimensionsInMetersToggler(bool v) {
177+
allMarkers = allMarkers.map(
178+
(c) {
179+
return Marker(
180+
point: c.point,
181+
useDimensionsInMeters: v ? const BoxConstraints() : null,
182+
height: v ? 1000 : 30,
183+
width: v ? 1000 : 30,
184+
child: SizedBox.expand(
185+
child: DecoratedBox(
186+
decoration: BoxDecoration(
187+
border: Border.all(),
188+
),
189+
),
190+
),
191+
);
192+
},
193+
).toList(growable: false);
194+
useSizeInMeters = v;
195+
setState(() {});
196+
}
197+
198+
void _useIconsToggler(bool v) {
199+
allMarkers = allMarkers
200+
.map(
201+
v
202+
? (c) => Marker(
203+
point: c.point,
204+
child: Icon(
205+
Icons.location_pin,
206+
size: 30,
207+
color: Color.fromARGB(
208+
255,
209+
randomGenerator.nextInt(256),
210+
randomGenerator.nextInt(256),
211+
randomGenerator.nextInt(256),
212+
),
213+
),
214+
)
215+
: (c) => Marker(
216+
point: c.point,
217+
useDimensionsInMeters:
218+
useSizeInMeters ? const BoxConstraints() : null,
219+
height: useSizeInMeters ? 1000 : 30,
220+
width: useSizeInMeters ? 1000 : 30,
221+
child: SizedBox.expand(
222+
child: DecoratedBox(
223+
decoration: BoxDecoration(
224+
border: Border.all(),
225+
),
226+
),
227+
),
228+
),
229+
)
230+
.toList(growable: false);
231+
useIcons = v;
232+
setState(() {});
233+
}
131234
}

0 commit comments

Comments
 (0)