11import 'package:flutter/material.dart' ;
22import 'package:maplibre/maplibre.dart' ;
3- import 'package:maplibre_example/map_styles.dart' ;
3+ import 'package:maplibre_example/utils/map_styles.dart' ;
4+ import 'package:maplibre_example/utils/style_dropdown.dart' ;
45
56@immutable
67class StyledMapPage extends StatefulWidget {
@@ -13,56 +14,31 @@ class StyledMapPage extends StatefulWidget {
1314}
1415
1516class _StyledMapPageState extends State <StyledMapPage > {
16- static const Map <String , String > _styles = {
17- 'MapTiler Streets' : MapStyles .maptilerStreets,
18- 'Protomaps Light' : MapStyles .protomapsLight,
19- 'Protomaps Dark' : MapStyles .protomapsDark,
20- 'Countries' : MapStyles .countries,
21- };
22- String _styleUrl = _styles.values.first;
17+ MapStyles _style = StyleDropdown .initStyle;
2318 late MapController _controller;
2419
2520 @override
2621 Widget build (BuildContext context) {
2722 return Scaffold (
2823 appBar: AppBar (
29- title: const Text ('Styled Map' ),
24+ title: const Text ('Map Styles ' ),
3025 bottom: PreferredSize (
3126 preferredSize: const Size .fromHeight (50 ),
32- child: Padding (
33- padding: const EdgeInsets .only (bottom: 16 , left: 8 , right: 8 ),
34- child: SizedBox (
35- height: 30 ,
36- child: ListView (
37- scrollDirection: Axis .horizontal,
38- shrinkWrap: true ,
39- children: _styles.entries
40- .map (
41- (e) => Padding (
42- padding: const EdgeInsets .symmetric (horizontal: 2 ),
43- child: ChoiceChip (
44- label: Text (e.key),
45- selected: _styleUrl == e.value,
46- onSelected: (value) {
47- // update the segmented button
48- setState (() => _styleUrl = e.value);
49- // apply the new style on the map
50- _controller.setStyle (_styleUrl);
51- },
52- ),
53- ),
54- )
55- .toList (growable: false ),
56- ),
57- ),
27+ child: StyleDropdown (
28+ onChanged: (style) {
29+ // update the segmented button
30+ setState (() => _style = style);
31+ // apply the new style on the map
32+ _controller.setStyle (style.uri);
33+ },
5834 ),
5935 ),
6036 ),
6137 body: MapLibreMap (
6238 options: MapOptions (
6339 initCenter: const Geographic (lon: 9.17 , lat: 47.68 ),
6440 initZoom: 2 ,
65- initStyle: _styleUrl ,
41+ initStyle: _style.uri ,
6642 ),
6743 children: const [
6844 MapScalebar (),
0 commit comments