-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathsnapshotter_example.dart
96 lines (82 loc) · 2.51 KB
/
snapshotter_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import 'package:flutter/material.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
import 'example.dart';
class SnapshotterExample extends StatefulWidget implements Example {
@override
final Widget leading = const Icon(Icons.camera_alt_outlined);
@override
final String title = 'Create a static map snapshot';
@override
final String subtitle =
"Create a static, non-interactive image of a map style with specified camera position.";
@override
State<StatefulWidget> createState() => SnapshotterExampleState();
}
class SnapshotterExampleState extends State<SnapshotterExample> {
SnapshotterExampleState();
GlobalKey _snapshotKey = GlobalKey();
MapboxMap? mapboxMap;
Image? snapshotImage;
Snapshotter? _snapshotter;
bool snapshotting = false;
@override
void dispose() {
_snapshotter?.dispose();
super.dispose();
}
_onMapCreated(MapboxMap mapboxMap) async {
this.mapboxMap = mapboxMap;
_snapshotter = await Snapshotter.create(
options: MapSnapshotOptions(
size: Size(width: 400, height: 400),
pixelRatio: MediaQuery.of(context).devicePixelRatio),
);
await _snapshotter?.style.setStyleURI(MapboxStyles.OUTDOORS);
}
_onMapIdle(MapIdleEventData data) async {
if (snapshotting) {
return;
}
snapshotting = true;
RenderBox snapshotBox =
_snapshotKey.currentContext!.findRenderObject() as RenderBox;
if (snapshotBox.hasSize) {
_snapshotter?.setSize(
Size(width: snapshotBox.size.width, height: snapshotBox.size.height));
}
final cameraState = await mapboxMap!.getCameraState();
_snapshotter?.setCamera(cameraState.toCameraOptions());
final snapshot = await _snapshotter?.start();
if (snapshot != null) {
setState(() {
snapshotImage = Image.memory(snapshot);
});
}
snapshotting = false;
}
@override
Widget build(BuildContext context) {
final MapWidget mapWidget = MapWidget(
key: ValueKey("mapWidget"),
onMapCreated: _onMapCreated,
onMapIdleListener: _onMapIdle,
);
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(child: mapWidget),
SizedBox(
height: 12,
child: ColoredBox(
color: Colors.amber,
),
),
Expanded(
key: _snapshotKey,
child: snapshotImage ?? ColoredBox(color: Colors.grey)),
],
),
);
}
}