1+ import 'dart:ui' ;
2+
13import 'package:flutter/foundation.dart' ;
4+ import 'package:flutter/services.dart' ;
5+ import 'package:flutter/widgets.dart' ;
26import 'package:maplibre/maplibre.dart' ;
37
48/// The [StyleController] can be used to manipulate the style of
59/// a [MapLibreMap] . It can be accessed via [MapController.style] .
610///
711/// {@category Basic}
8- abstract interface class StyleController {
12+ abstract class StyleController {
13+ /// Abstract base constructor for implementations.
14+ const StyleController ();
15+
916 /// Add a new source to the map.
1017 Future <void > addSource (Source source);
1118
1219 /// Add a new layer to the map. The source must be added before adding it to
1320 /// the map.
1421 ///
15- /// ` belowLayerId` The ID of an existing layer to insert the new layer before,
22+ /// [ belowLayerId] The ID of an existing layer to insert the new layer before,
1623 /// resulting in the new layer appearing visually beneath the existing layer.
17- /// If this argument is not specified, the layer will be appended to the end
18- /// of the layers array and appear visually above all other layers.
19- Future <void > addLayer (StyleLayer layer, {String ? belowLayerId});
24+ ///
25+ /// [aboveLayerId] The ID of an existing layer to insert the new layer after,
26+ /// resulting in the new layer appearing visually above the existing layer.
27+ /// **This parameter will be ignored on web.**
28+ ///
29+ /// [atIndex] is the position at which to insert the layer. An index of 0
30+ /// would send the layer to the back; an index equal to the number of
31+ /// objects in the layers property would bring the layer to the front.
32+ /// **This parameter will be ignored on web.**
33+ ///
34+ /// If more than one positioning parameter is specified, it will first try to
35+ /// use [belowLayerId] , then [aboveLayerId] and finally [atIndex] .
36+ /// If no positioning parameter is specified, the layer
37+ /// will be appended to the end of the layers array and appear visually
38+ /// above all other layers.
39+ Future <void > addLayer (
40+ StyleLayer layer, {
41+ String ? belowLayerId,
42+ String ? aboveLayerId,
43+ int ? atIndex,
44+ });
2045
2146 /// Update the data of a GeoJSON source.
2247 Future <void > updateGeoJsonSource ({required String id, required String data});
2348
24- /// Removes the layer with the given ID from the map's style.
49+ /// Removes the layer with the given [id] from the map's style.
2550 Future <void > removeLayer (String id);
2651
27- /// Removes the source with the given ID from the map's style.
52+ /// Removes the source with the given [id] from the map's style.
2853 Future <void > removeSource (String id);
2954
3055 /// Get a list of all attributions from the map style.
@@ -40,6 +65,60 @@ abstract interface class StyleController {
4065 /// Add an image to the map.
4166 Future <void > addImage (String id, Uint8List bytes);
4267
68+ /// Add multiple images to the map where the key is the image ID and the
69+ /// value is the image bytes.
70+ Future <void > addImages (Map <String , Uint8List > images) => Future .wait (
71+ images.entries.map ((e) => addImage (e.key, e.value)),
72+ );
73+
74+ /// Load an image from the Flutter assets to the map by its [asset] path.
75+ Future <void > addImageFromAssets ({
76+ required String id,
77+ required String asset,
78+ }) async {
79+ final byteData = await rootBundle.load (asset);
80+ final bytes = byteData.buffer.asUint8List ();
81+ await addImage (id, bytes);
82+ }
83+
84+ /// Create an image from [IconData] and add it to the map with the given [id] .
85+ ///
86+ /// The [size] parameter defines the width and height of the resulting image
87+ /// in pixels.
88+ ///
89+ /// The [color] parameter defines the color of the icon. By default, it is
90+ /// black.
91+ Future <void > addImageFromIconData ({
92+ required String id,
93+ required IconData iconData,
94+ int size = 200 ,
95+ Color color = const Color (0xFF000000 ),
96+ }) async {
97+ final pictureRecorder = PictureRecorder ();
98+ final canvas = Canvas (pictureRecorder);
99+
100+ TextPainter (textDirection: TextDirection .ltr)
101+ ..text = TextSpan (
102+ text: String .fromCharCode (iconData.codePoint),
103+ style: TextStyle (
104+ letterSpacing: 0 ,
105+ fontSize: size.toDouble (),
106+ fontFamily: iconData.fontFamily,
107+ package: iconData.fontPackage,
108+ color: color,
109+ ),
110+ )
111+ ..layout ()
112+ ..paint (canvas, Offset .zero);
113+
114+ final picture = pictureRecorder.endRecording ();
115+ final image = await picture.toImage (size, size);
116+ final bytes = await image.toByteData (format: ImageByteFormat .png);
117+ if (bytes == null ) return ;
118+
119+ await addImage (id, bytes.buffer.asUint8List ());
120+ }
121+
43122 /// Removes an image from the map
44123 Future <void > removeImage (String id);
45124
0 commit comments