-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmapkit_snapshotter_image.dart
More file actions
87 lines (77 loc) · 2.56 KB
/
mapkit_snapshotter_image.dart
File metadata and controls
87 lines (77 loc) · 2.56 KB
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
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/services.dart';
import 'package:mapkit_snapshotter_flutter/src/mapkit_snapshotter_types.dart';
/// Provides the MapKit Snapshow based on the given [options].
///
/// Usage:
/// ```dart
/// final widget = Image(
/// image: MapKitSnapshotterImage(
/// MapKitSnapshotterOptions(
/// /// Defines the region to capture.
/// region: MapKitSnapshotterRegion(
/// centerLatitude: 53.552363,
/// centerLongitude: 9.990831,
/// latitudeMeters: 2000,
/// longitudeMeters: 2000,
/// ),
/// /// Additional properties for the snapshot.
/// brightness: MapKitSnapshotterBrightness.light,
/// mapType: MapKitSnapshotterMapType.hybridFlyover,
/// ),
/// ),
/// height: 100,
/// width: 100,
/// );
/// ```
class MapKitSnapshotterImage
extends ImageProvider<MapKitSnapshotterImageProviderKey> {
static const MethodChannel _channel =
MethodChannel('mapkit_snapshotter_flutter');
/// Defines the options for the capture. This is required as it contains
/// information about the region to capture.
final MapKitSnapshotterOptions options;
MapKitSnapshotterImage(this.options);
@override
ImageStreamCompleter loadImage(
MapKitSnapshotterImageProviderKey key,
decode,
) {
return OneFrameImageStreamCompleter(Future(() async {
// Capture the screenshot on the iOS native side.
final captureResponse = await _channel.invokeMethod(
'capture',
key.toJson(),
);
// Return null if the response si null.
if (captureResponse == null) {
throw StateError('Failed to take snapshot');
}
final desc = await ui.ImageDescriptor.encoded(
await ui.ImmutableBuffer.fromUint8List(captureResponse),
);
final codec = await desc.instantiateCodec();
final frame = await codec.getNextFrame();
final image = frame.image;
return ImageInfo(
image: image,
scale: key.devicePixelRatio,
);
}));
}
@override
Future<MapKitSnapshotterImageProviderKey> obtainKey(
ImageConfiguration configuration,
) {
// The configuration does not always provide all values, therefore we need
// to define some defaults.
return Future.value(MapKitSnapshotterImageProviderKey(
sizeHeight: configuration.size?.height ?? 400,
sizeWidth: configuration.size?.width ?? 400,
devicePixelRatio: configuration.devicePixelRatio ?? 1,
options: options,
));
}
}