Skip to content

Commit c3a2ea2

Browse files
add switch style button
1 parent 4abdb98 commit c3a2ea2

File tree

1 file changed

+73
-45
lines changed

1 file changed

+73
-45
lines changed

example/lib/basemap_style_example_page.dart

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class _BasemapStyleExamplePageState extends State<BasemapStyleExamplePage> {
1919
/// null when executed on a platform that's not supported yet
2020
ArcgisMapController? _controller;
2121
BaseMap selectedBasemap = BaseMap.values.first;
22+
var _isSwitchingAllStyles = false;
2223
bool show3dMap = false;
2324
bool isBasemapMenuOpened = false;
2425
final initialCenter = const LatLng(51.16, 10.45);
@@ -63,7 +64,6 @@ class _BasemapStyleExamplePageState extends State<BasemapStyleExamplePage> {
6364
@override
6465
Widget build(BuildContext context) {
6566
return Scaffold(
66-
// appBar: AppBar(),
6767
key: _scaffoldKey,
6868
body: GestureDetector(
6969
onTap: () {
@@ -170,6 +170,14 @@ class _BasemapStyleExamplePageState extends State<BasemapStyleExamplePage> {
170170
],
171171
),
172172
SizedBox(height: 8),
173+
Center(
174+
child: ElevatedButton(
175+
onPressed:
176+
_isSwitchingAllStyles ? null : _switchAllStyles,
177+
child: Text(_isSwitchingAllStyles
178+
? "Switching... ${BaseMap.values.indexOf(selectedBasemap) + 1}/${BaseMap.values.length}"
179+
: "Switch through all styles once")),
180+
),
173181
Center(
174182
child: ElevatedButton(
175183
style: ButtonStyle(
@@ -178,53 +186,56 @@ class _BasemapStyleExamplePageState extends State<BasemapStyleExamplePage> {
178186
borderRadius: BorderRadius.circular(8)),
179187
),
180188
),
181-
onPressed: () {
182-
showModalBottomSheet(
183-
context: context,
184-
builder: (context) {
185-
return PointerInterceptorWeb(
186-
child: ListView.separated(
187-
padding: EdgeInsets.only(
188-
top: 8,
189-
bottom:
190-
MediaQuery.paddingOf(context).bottom +
191-
8),
192-
itemCount: BaseMap.values.length,
193-
separatorBuilder: (_, __) => Padding(
194-
padding: const EdgeInsets.symmetric(
195-
horizontal: 20),
196-
child: Divider(
197-
height: 1,
198-
),
199-
),
200-
itemBuilder: (context, int i) {
201-
final basemap = BaseMap.values[i];
202-
return ListTile(
203-
dense: true,
204-
onTap: () {
205-
Navigator.pop(context);
206-
setState(() {
207-
selectedBasemap = basemap;
208-
});
209-
210-
_controller!
211-
.toggleBaseMap(baseMap: basemap);
212-
},
213-
title: Text(
214-
basemap.name,
215-
style: TextStyle(
216-
color: Theme.of(context)
217-
.buttonTheme
218-
.colorScheme!
219-
.onPrimaryContainer,
189+
onPressed: _isSwitchingAllStyles
190+
? null
191+
: () {
192+
showModalBottomSheet(
193+
context: context,
194+
builder: (context) {
195+
return PointerInterceptorWeb(
196+
child: ListView.separated(
197+
padding: EdgeInsets.only(
198+
top: 8,
199+
bottom:
200+
MediaQuery.paddingOf(context)
201+
.bottom +
202+
8),
203+
itemCount: BaseMap.values.length,
204+
separatorBuilder: (_, __) => Padding(
205+
padding: const EdgeInsets.symmetric(
206+
horizontal: 20),
207+
child: Divider(
208+
height: 1,
209+
),
220210
),
211+
itemBuilder: (context, int i) {
212+
final basemap = BaseMap.values[i];
213+
return ListTile(
214+
dense: true,
215+
onTap: () {
216+
Navigator.pop(context);
217+
setState(() {
218+
selectedBasemap = basemap;
219+
});
220+
221+
_controller!.toggleBaseMap(
222+
baseMap: basemap);
223+
},
224+
title: Text(
225+
basemap.name,
226+
style: TextStyle(
227+
color: Theme.of(context)
228+
.buttonTheme
229+
.colorScheme!
230+
.onPrimaryContainer,
231+
),
232+
),
233+
);
234+
},
221235
),
222236
);
223-
},
224-
),
225-
);
226-
});
227-
},
237+
});
238+
},
228239
child: Row(
229240
mainAxisSize: MainAxisSize.min,
230241
children: [
@@ -264,4 +275,21 @@ class _BasemapStyleExamplePageState extends State<BasemapStyleExamplePage> {
264275
),
265276
);
266277
}
278+
279+
Future<void> _switchAllStyles() async {
280+
setState(() => _isSwitchingAllStyles = true);
281+
try {
282+
for (final baseMap in BaseMap.values) {
283+
await _controller?.toggleBaseMap(baseMap: baseMap);
284+
if (!mounted) return;
285+
setState(() => selectedBasemap = baseMap);
286+
await Future<void>.delayed(const Duration(seconds: 2));
287+
if (!mounted) return;
288+
}
289+
} finally {
290+
if (mounted) {
291+
setState(() => _isSwitchingAllStyles = false);
292+
}
293+
}
294+
}
267295
}

0 commit comments

Comments
 (0)