Skip to content

Commit

Permalink
[Example]Add linePattern in line.dart (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Li authored Dec 21, 2021
1 parent 0feb12b commit 0c13b66
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions example/lib/line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:mapbox_gl/mapbox_gl.dart';

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

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

/// Adds an asset image to the currently displayed style
Future<void> 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"),
Expand Down Expand Up @@ -100,6 +110,14 @@ class LineBodyState extends State<LineBody> {
});
}

Future<void> _changeLinePattern() async {
String? current =
_selectedLine!.options.linePattern == null ? "assetImage" : null;
await _updateSelectedLine(
LineOptions(linePattern: current),
);
}

Future<void> _changeAlpha() async {
double? current = _selectedLine!.options.lineOpacity;
if (current == null) {
Expand All @@ -124,6 +142,7 @@ class LineBodyState extends State<LineBody> {
}

_onStyleLoadedCallback() async {
addImageFromAsset("assetImage", _linePatternImage);
await controller!.addLine(
LineOptions(
geometry: [LatLng(37.4220, -122.0841), LatLng(37.4240, -122.0941)],
Expand Down Expand Up @@ -179,6 +198,12 @@ class LineBodyState extends State<LineBody> {
await _move();
},
),
TextButton(
child: const Text('change line-pattern'),
onPressed: (_selectedLine == null)
? null
: _changeLinePattern,
),
],
),
Row(
Expand Down

0 comments on commit 0c13b66

Please sign in to comment.