diff --git a/example/lib/line.dart b/example/lib/line.dart index 13b34ad5..42512073 100644 --- a/example/lib/line.dart +++ b/example/lib/line.dart @@ -3,8 +3,10 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:typed_data'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:maplibre_gl/mapbox_gl.dart'; import 'page.dart'; @@ -33,6 +35,7 @@ class LineBodyState extends State { MaplibreMapController? controller; int _lineCount = 0; Line? _selectedLine; + final String _linePatternImage = "assets/fill/cat_silhouette_pattern.png"; void _onMapCreated(MaplibreMapController controller) { this.controller = controller; @@ -45,6 +48,13 @@ class LineBodyState extends State { super.dispose(); } + /// Adds an asset image to the currently displayed style + Future addImageFromAsset(String name, String assetName) async { + final ByteData bytes = await rootBundle.load(assetName); + final Uint8List list = bytes.buffer.asUint8List(); + return controller!.addImage(name, list); + } + _onLineTapped(Line line) async { await _updateSelectedLine( LineOptions(lineColor: "#ff0000"), @@ -99,6 +109,14 @@ class LineBodyState extends State { }); } + Future _changeLinePattern() async { + String? current = + _selectedLine!.options.linePattern == null ? "assetImage" : null; + await _updateSelectedLine( + LineOptions(linePattern: current), + ); + } + Future _changeAlpha() async { double? current = _selectedLine!.options.lineOpacity; if (current == null) { @@ -123,6 +141,7 @@ class LineBodyState extends State { } _onStyleLoadedCallback() async { + addImageFromAsset("assetImage", _linePatternImage); await controller!.addLine( LineOptions( geometry: [LatLng(37.4220, -122.0841), LatLng(37.4240, -122.0941)], @@ -177,6 +196,12 @@ class LineBodyState extends State { await _move(); }, ), + TextButton( + child: const Text('change line-pattern'), + onPressed: (_selectedLine == null) + ? null + : _changeLinePattern, + ), ], ), Row(