|
| 1 | +import 'dart:convert'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart'; |
| 4 | +import 'example.dart'; |
| 5 | + |
| 6 | +class StandardStyleImportExample extends StatefulWidget implements Example { |
| 7 | + @override |
| 8 | + final Widget leading = const Icon(Icons.touch_app); |
| 9 | + @override |
| 10 | + final String title = 'Standard Style Import'; |
| 11 | + @override |
| 12 | + final String? subtitle = 'Configure the Standard Style and add interactions'; |
| 13 | + |
| 14 | + const StandardStyleImportExample({super.key}); |
| 15 | + |
| 16 | + @override |
| 17 | + State<StatefulWidget> createState() => StandardStyleImportState(); |
| 18 | +} |
| 19 | + |
| 20 | +class StandardStyleImportState extends State<StandardStyleImportExample> { |
| 21 | + StandardStyleImportState(); |
| 22 | + MapboxMap? mapboxMap; |
| 23 | + |
| 24 | + /// Style import config properties |
| 25 | + String lightPreset = 'day'; |
| 26 | + bool labelsSetting = true; |
| 27 | + |
| 28 | + _onMapCreated(MapboxMap mapboxMap) { |
| 29 | + this.mapboxMap = mapboxMap; |
| 30 | + mapboxMap.style; |
| 31 | + |
| 32 | + // When the map is ready, add a tap interaction to show a snackbar with the name of the place that was tapped |
| 33 | + mapboxMap |
| 34 | + .addInteraction(TapInteraction(StandardPlaceLabels(), (feature, _) { |
| 35 | + ScaffoldMessenger.of(context).showSnackBar( |
| 36 | + SnackBar(content: Text("Tapped place: ${feature.name}")), |
| 37 | + ); |
| 38 | + })); |
| 39 | + } |
| 40 | + |
| 41 | + @override |
| 42 | + Widget build(BuildContext context) { |
| 43 | + return Scaffold( |
| 44 | + floatingActionButton: Column( |
| 45 | + mainAxisAlignment: MainAxisAlignment.end, |
| 46 | + children: [ |
| 47 | + FloatingActionButton( |
| 48 | + heroTag: 'light', |
| 49 | + onPressed: _changeLightSetting, |
| 50 | + child: Icon(Icons.wb_sunny), |
| 51 | + ), |
| 52 | + SizedBox(height: 16), |
| 53 | + FloatingActionButton( |
| 54 | + heroTag: 'labels', |
| 55 | + onPressed: _changeLabelsSetting, |
| 56 | + child: Icon(Icons.label), |
| 57 | + ), |
| 58 | + ], |
| 59 | + ), |
| 60 | + body: Stack(children: [ |
| 61 | + MapWidget( |
| 62 | + key: ValueKey("mapWidget"), |
| 63 | + cameraOptions: CameraOptions( |
| 64 | + center: Point(coordinates: Position(-73.99, 40.72)), |
| 65 | + zoom: 11, |
| 66 | + pitch: 45), |
| 67 | + styleUri: MapboxStyles.STANDARD, |
| 68 | + textureView: true, |
| 69 | + onMapCreated: _onMapCreated, |
| 70 | + onStyleLoadedListener: _onStyleLoaded, |
| 71 | + ) |
| 72 | + ])); |
| 73 | + } |
| 74 | + |
| 75 | + _onStyleLoaded(StyleLoadedEventData styleLoadedEventData) { |
| 76 | + // When the style has finished loading add a line layer representing the border between New York and New Jersey |
| 77 | + _addLineLayer(); |
| 78 | + } |
| 79 | + |
| 80 | + void _addLineLayer() { |
| 81 | + final lineLayer = LineLayer( |
| 82 | + id: 'line-layer', |
| 83 | + sourceId: 'line-layer', |
| 84 | + lineColor: Colors.orange.value, |
| 85 | + lineWidth: 8, |
| 86 | + ); |
| 87 | + |
| 88 | + final line = LineString(coordinates: [ |
| 89 | + Position(-73.91912400100642, 40.913503418907936), |
| 90 | + Position(-73.9615887363045, 40.82943110786286), |
| 91 | + Position(-74.01409059085539, 40.75461056309348), |
| 92 | + Position(-74.02798814058939, 40.69522028220487), |
| 93 | + Position(-74.05655532615407, 40.65188756398558), |
| 94 | + Position(-74.13916853846217, 40.64339339389301), |
| 95 | + ]); |
| 96 | + |
| 97 | + final lineSource = GeoJsonSource( |
| 98 | + id: 'line-layer', |
| 99 | + data: json.encode(line), |
| 100 | + ); |
| 101 | + |
| 102 | + mapboxMap?.style.addSource(lineSource); |
| 103 | + mapboxMap?.style.addLayer(lineLayer); |
| 104 | + } |
| 105 | + |
| 106 | + void _changeLightSetting() { |
| 107 | + final presets = ['dawn', 'day', 'dusk', 'night']; |
| 108 | + final currentIndex = presets.indexOf(lightPreset); |
| 109 | + setState(() { |
| 110 | + lightPreset = presets[(currentIndex + 1) % presets.length]; |
| 111 | + _updateMapStyle(); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + // Toggle the visibility of the labels |
| 116 | + void _changeLabelsSetting() { |
| 117 | + setState(() { |
| 118 | + labelsSetting = !labelsSetting; |
| 119 | + _updateMapStyle(); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + void _updateMapStyle() { |
| 124 | + // Update the map style's config properties based on the selected options |
| 125 | + var configs = { |
| 126 | + "lightPreset": lightPreset, |
| 127 | + "showPointOfInterestLabels": labelsSetting, |
| 128 | + "showTransitLabels": labelsSetting, |
| 129 | + "showPlaceLabels": labelsSetting, |
| 130 | + }; |
| 131 | + mapboxMap?.style.setStyleImportConfigProperties("basemap", configs); |
| 132 | + } |
| 133 | +} |
0 commit comments