3
3
// found in the LICENSE file.
4
4
5
5
import 'package:camera_platform_interface/src/types/focus_mode.dart' ;
6
+ import 'package:meta/meta.dart' ;
6
7
7
8
import '../../camera_platform_interface.dart' ;
8
9
@@ -22,14 +23,15 @@ import '../../camera_platform_interface.dart';
22
23
/// See below for examples: `CameraClosingEvent` , `CameraErrorEvent` ...
23
24
/// These events are more semantic and more pleasant to use than raw generics.
24
25
/// They can be (and in fact, are) filtered by the `instanceof` -operator.
26
+ @immutable
25
27
abstract class CameraEvent {
26
- /// The ID of the Camera this event is associated to.
27
- final int cameraId;
28
-
29
28
/// Build a Camera Event, that relates a `cameraId` .
30
29
///
31
30
/// The `cameraId` is the ID of the camera that triggered the event.
32
- CameraEvent (this .cameraId) : assert (cameraId != null );
31
+ const CameraEvent (this .cameraId) : assert (cameraId != null );
32
+
33
+ /// The ID of the Camera this event is associated to.
34
+ final int cameraId;
33
35
34
36
@override
35
37
bool operator == (Object other) =>
@@ -44,30 +46,12 @@ abstract class CameraEvent {
44
46
45
47
/// An event fired when the camera has finished initializing.
46
48
class CameraInitializedEvent extends CameraEvent {
47
- /// The width of the preview in pixels.
48
- final double previewWidth;
49
-
50
- /// The height of the preview in pixels.
51
- final double previewHeight;
52
-
53
- /// The default exposure mode
54
- final ExposureMode exposureMode;
55
-
56
- /// The default focus mode
57
- final FocusMode focusMode;
58
-
59
- /// Whether setting exposure points is supported.
60
- final bool exposurePointSupported;
61
-
62
- /// Whether setting focus points is supported.
63
- final bool focusPointSupported;
64
-
65
49
/// Build a CameraInitialized event triggered from the camera represented by
66
50
/// `cameraId` .
67
51
///
68
52
/// The `previewWidth` represents the width of the generated preview in pixels.
69
53
/// The `previewHeight` represents the height of the generated preview in pixels.
70
- CameraInitializedEvent (
54
+ const CameraInitializedEvent (
71
55
int cameraId,
72
56
this .previewWidth,
73
57
this .previewHeight,
@@ -80,17 +64,36 @@ class CameraInitializedEvent extends CameraEvent {
80
64
/// Converts the supplied [Map] to an instance of the [CameraInitializedEvent]
81
65
/// class.
82
66
CameraInitializedEvent .fromJson (Map <String , dynamic > json)
83
- : previewWidth = json['previewWidth' ],
84
- previewHeight = json['previewHeight' ],
85
- exposureMode = deserializeExposureMode (json['exposureMode' ]),
86
- exposurePointSupported = json['exposurePointSupported' ] ?? false ,
87
- focusMode = deserializeFocusMode (json['focusMode' ]),
88
- focusPointSupported = json['focusPointSupported' ] ?? false ,
89
- super (json['cameraId' ]);
67
+ : previewWidth = json['previewWidth' ]! as double ,
68
+ previewHeight = json['previewHeight' ]! as double ,
69
+ exposureMode = deserializeExposureMode (json['exposureMode' ]! as String ),
70
+ exposurePointSupported =
71
+ (json['exposurePointSupported' ] as bool ? ) ?? false ,
72
+ focusMode = deserializeFocusMode (json['focusMode' ]! as String ),
73
+ focusPointSupported = (json['focusPointSupported' ] as bool ? ) ?? false ,
74
+ super (json['cameraId' ]! as int );
75
+
76
+ /// The width of the preview in pixels.
77
+ final double previewWidth;
78
+
79
+ /// The height of the preview in pixels.
80
+ final double previewHeight;
81
+
82
+ /// The default exposure mode
83
+ final ExposureMode exposureMode;
84
+
85
+ /// The default focus mode
86
+ final FocusMode focusMode;
87
+
88
+ /// Whether setting exposure points is supported.
89
+ final bool exposurePointSupported;
90
+
91
+ /// Whether setting focus points is supported.
92
+ final bool focusPointSupported;
90
93
91
94
/// Converts the [CameraInitializedEvent] instance into a [Map] instance that
92
95
/// can be serialized to JSON.
93
- Map <String , dynamic > toJson () => {
96
+ Map <String , dynamic > toJson () => < String , Object > {
94
97
'cameraId' : cameraId,
95
98
'previewWidth' : previewWidth,
96
99
'previewHeight' : previewHeight,
@@ -126,18 +129,12 @@ class CameraInitializedEvent extends CameraEvent {
126
129
127
130
/// An event fired when the resolution preset of the camera has changed.
128
131
class CameraResolutionChangedEvent extends CameraEvent {
129
- /// The capture width in pixels.
130
- final double captureWidth;
131
-
132
- /// The capture height in pixels.
133
- final double captureHeight;
134
-
135
132
/// Build a CameraResolutionChanged event triggered from the camera
136
133
/// represented by `cameraId` .
137
134
///
138
135
/// The `captureWidth` represents the width of the resulting image in pixels.
139
136
/// The `captureHeight` represents the height of the resulting image in pixels.
140
- CameraResolutionChangedEvent (
137
+ const CameraResolutionChangedEvent (
141
138
int cameraId,
142
139
this .captureWidth,
143
140
this .captureHeight,
@@ -146,13 +143,19 @@ class CameraResolutionChangedEvent extends CameraEvent {
146
143
/// Converts the supplied [Map] to an instance of the
147
144
/// [CameraResolutionChangedEvent] class.
148
145
CameraResolutionChangedEvent .fromJson (Map <String , dynamic > json)
149
- : captureWidth = json['captureWidth' ],
150
- captureHeight = json['captureHeight' ],
151
- super (json['cameraId' ]);
146
+ : captureWidth = json['captureWidth' ]! as double ,
147
+ captureHeight = json['captureHeight' ]! as double ,
148
+ super (json['cameraId' ]! as int );
149
+
150
+ /// The capture width in pixels.
151
+ final double captureWidth;
152
+
153
+ /// The capture height in pixels.
154
+ final double captureHeight;
152
155
153
156
/// Converts the [CameraResolutionChangedEvent] instance into a [Map] instance
154
157
/// that can be serialized to JSON.
155
- Map <String , dynamic > toJson () => {
158
+ Map <String , dynamic > toJson () => < String , Object > {
156
159
'cameraId' : cameraId,
157
160
'captureWidth' : captureWidth,
158
161
'captureHeight' : captureHeight,
@@ -162,7 +165,7 @@ class CameraResolutionChangedEvent extends CameraEvent {
162
165
bool operator == (Object other) =>
163
166
identical (this , other) ||
164
167
other is CameraResolutionChangedEvent &&
165
- super == ( other) &&
168
+ super == other &&
166
169
runtimeType == other.runtimeType &&
167
170
captureWidth == other.captureWidth &&
168
171
captureHeight == other.captureHeight;
@@ -176,58 +179,61 @@ class CameraResolutionChangedEvent extends CameraEvent {
176
179
class CameraClosingEvent extends CameraEvent {
177
180
/// Build a CameraClosing event triggered from the camera represented by
178
181
/// `cameraId` .
179
- CameraClosingEvent (int cameraId) : super (cameraId);
182
+ const CameraClosingEvent (int cameraId) : super (cameraId);
180
183
181
184
/// Converts the supplied [Map] to an instance of the [CameraClosingEvent]
182
185
/// class.
183
186
CameraClosingEvent .fromJson (Map <String , dynamic > json)
184
- : super (json['cameraId' ]);
187
+ : super (json['cameraId' ]! as int );
185
188
186
189
/// Converts the [CameraClosingEvent] instance into a [Map] instance that can
187
190
/// be serialized to JSON.
188
- Map <String , dynamic > toJson () => {
191
+ Map <String , dynamic > toJson () => < String , Object > {
189
192
'cameraId' : cameraId,
190
193
};
191
194
192
195
@override
193
196
bool operator == (Object other) =>
194
197
identical (this , other) ||
195
- super == ( other) &&
198
+ super == other &&
196
199
other is CameraClosingEvent &&
197
200
runtimeType == other.runtimeType;
198
201
199
202
@override
203
+ // This is here even though it just calls super to make it less likely that
204
+ // operator== would be changed without changing `hashCode`.
205
+ // ignore: unnecessary_overrides
200
206
int get hashCode => super .hashCode;
201
207
}
202
208
203
209
/// An event fired when an error occured while operating the camera.
204
210
class CameraErrorEvent extends CameraEvent {
205
- /// Description of the error.
206
- final String description;
207
-
208
211
/// Build a CameraError event triggered from the camera represented by
209
212
/// `cameraId` .
210
213
///
211
214
/// The `description` represents the error occured on the camera.
212
- CameraErrorEvent (int cameraId, this .description) : super (cameraId);
215
+ const CameraErrorEvent (int cameraId, this .description) : super (cameraId);
213
216
214
217
/// Converts the supplied [Map] to an instance of the [CameraErrorEvent]
215
218
/// class.
216
219
CameraErrorEvent .fromJson (Map <String , dynamic > json)
217
- : description = json['description' ],
218
- super (json['cameraId' ]);
220
+ : description = json['description' ]! as String ,
221
+ super (json['cameraId' ]! as int );
222
+
223
+ /// Description of the error.
224
+ final String description;
219
225
220
226
/// Converts the [CameraErrorEvent] instance into a [Map] instance that can be
221
227
/// serialized to JSON.
222
- Map <String , dynamic > toJson () => {
228
+ Map <String , dynamic > toJson () => < String , Object > {
223
229
'cameraId' : cameraId,
224
230
'description' : description,
225
231
};
226
232
227
233
@override
228
234
bool operator == (Object other) =>
229
235
identical (this , other) ||
230
- super == ( other) &&
236
+ super == other &&
231
237
other is CameraErrorEvent &&
232
238
runtimeType == other.runtimeType &&
233
239
description == other.description;
@@ -238,32 +244,32 @@ class CameraErrorEvent extends CameraEvent {
238
244
239
245
/// An event fired when a video has finished recording.
240
246
class VideoRecordedEvent extends CameraEvent {
241
- /// XFile of the recorded video.
242
- final XFile file;
243
-
244
- /// Maximum duration of the recorded video.
245
- final Duration ? maxVideoDuration;
246
-
247
247
/// Build a VideoRecordedEvent triggered from the camera with the `cameraId` .
248
248
///
249
249
/// The `file` represents the file of the video.
250
250
/// The `maxVideoDuration` shows if a maxVideoDuration shows if a maximum
251
251
/// video duration was set.
252
- VideoRecordedEvent (int cameraId, this .file, this .maxVideoDuration)
252
+ const VideoRecordedEvent (int cameraId, this .file, this .maxVideoDuration)
253
253
: super (cameraId);
254
254
255
255
/// Converts the supplied [Map] to an instance of the [VideoRecordedEvent]
256
256
/// class.
257
257
VideoRecordedEvent .fromJson (Map <String , dynamic > json)
258
- : file = XFile (json['path' ]),
258
+ : file = XFile (json['path' ]! as String ),
259
259
maxVideoDuration = json['maxVideoDuration' ] != null
260
260
? Duration (milliseconds: json['maxVideoDuration' ] as int )
261
261
: null ,
262
- super (json['cameraId' ]);
262
+ super (json['cameraId' ]! as int );
263
+
264
+ /// XFile of the recorded video.
265
+ final XFile file;
266
+
267
+ /// Maximum duration of the recorded video.
268
+ final Duration ? maxVideoDuration;
263
269
264
270
/// Converts the [VideoRecordedEvent] instance into a [Map] instance that can be
265
271
/// serialized to JSON.
266
- Map <String , dynamic > toJson () => {
272
+ Map <String , dynamic > toJson () => < String , Object ? > {
267
273
'cameraId' : cameraId,
268
274
'path' : file.path,
269
275
'maxVideoDuration' : maxVideoDuration? .inMilliseconds
0 commit comments