Skip to content

Commit 44c529d

Browse files
author
Kevin Li
committed
[Example]Add linePattern in line.dart
1 parent 01a038d commit 44c529d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

example/lib/line.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:typed_data';
67

78
import 'package:flutter/material.dart';
9+
import 'package:flutter/services.dart';
810
import 'package:mapbox_gl/mapbox_gl.dart';
911

1012
import 'main.dart';
@@ -34,6 +36,7 @@ class LineBodyState extends State<LineBody> {
3436
MapboxMapController? controller;
3537
int _lineCount = 0;
3638
Line? _selectedLine;
39+
final String _linePatternImage = "assets/fill/cat_silhouette_pattern.png";
3740

3841
void _onMapCreated(MapboxMapController controller) {
3942
this.controller = controller;
@@ -46,6 +49,13 @@ class LineBodyState extends State<LineBody> {
4649
super.dispose();
4750
}
4851

52+
/// Adds an asset image to the currently displayed style
53+
Future<void> addImageFromAsset(String name, String assetName) async {
54+
final ByteData bytes = await rootBundle.load(assetName);
55+
final Uint8List list = bytes.buffer.asUint8List();
56+
return controller!.addImage(name, list);
57+
}
58+
4959
_onLineTapped(Line line) async {
5060
await _updateSelectedLine(
5161
LineOptions(lineColor: "#ff0000"),
@@ -100,6 +110,14 @@ class LineBodyState extends State<LineBody> {
100110
});
101111
}
102112

113+
Future<void> _changeLinePattern() async {
114+
String? current =
115+
_selectedLine!.options.linePattern == null ? "assetImage" : null;
116+
await _updateSelectedLine(
117+
LineOptions(linePattern: current),
118+
);
119+
}
120+
103121
Future<void> _changeAlpha() async {
104122
double? current = _selectedLine!.options.lineOpacity;
105123
if (current == null) {
@@ -124,6 +142,7 @@ class LineBodyState extends State<LineBody> {
124142
}
125143

126144
_onStyleLoadedCallback() async {
145+
addImageFromAsset("assetImage", _linePatternImage);
127146
await controller!.addLine(
128147
LineOptions(
129148
geometry: [LatLng(37.4220, -122.0841), LatLng(37.4240, -122.0941)],
@@ -179,6 +198,12 @@ class LineBodyState extends State<LineBody> {
179198
await _move();
180199
},
181200
),
201+
TextButton(
202+
child: const Text('change line-pattern'),
203+
onPressed: (_selectedLine == null)
204+
? null
205+
: _changeLinePattern,
206+
),
182207
],
183208
),
184209
Row(

0 commit comments

Comments
 (0)