33// found in the LICENSE file.
44
55import 'package:camera_platform_interface/src/types/focus_mode.dart' ;
6+ import 'package:meta/meta.dart' ;
67
78import '../../camera_platform_interface.dart' ;
89
@@ -22,14 +23,15 @@ import '../../camera_platform_interface.dart';
2223/// See below for examples: `CameraClosingEvent` , `CameraErrorEvent` ...
2324/// These events are more semantic and more pleasant to use than raw generics.
2425/// They can be (and in fact, are) filtered by the `instanceof` -operator.
26+ @immutable
2527abstract class CameraEvent {
26- /// The ID of the Camera this event is associated to.
27- final int cameraId;
28-
2928 /// Build a Camera Event, that relates a `cameraId` .
3029 ///
3130 /// 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;
3335
3436 @override
3537 bool operator == (Object other) =>
@@ -44,30 +46,12 @@ abstract class CameraEvent {
4446
4547/// An event fired when the camera has finished initializing.
4648class 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-
6549 /// Build a CameraInitialized event triggered from the camera represented by
6650 /// `cameraId` .
6751 ///
6852 /// The `previewWidth` represents the width of the generated preview in pixels.
6953 /// The `previewHeight` represents the height of the generated preview in pixels.
70- CameraInitializedEvent (
54+ const CameraInitializedEvent (
7155 int cameraId,
7256 this .previewWidth,
7357 this .previewHeight,
@@ -80,17 +64,36 @@ class CameraInitializedEvent extends CameraEvent {
8064 /// Converts the supplied [Map] to an instance of the [CameraInitializedEvent]
8165 /// class.
8266 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;
9093
9194 /// Converts the [CameraInitializedEvent] instance into a [Map] instance that
9295 /// can be serialized to JSON.
93- Map <String , dynamic > toJson () => {
96+ Map <String , dynamic > toJson () => < String , Object > {
9497 'cameraId' : cameraId,
9598 'previewWidth' : previewWidth,
9699 'previewHeight' : previewHeight,
@@ -126,18 +129,12 @@ class CameraInitializedEvent extends CameraEvent {
126129
127130/// An event fired when the resolution preset of the camera has changed.
128131class 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-
135132 /// Build a CameraResolutionChanged event triggered from the camera
136133 /// represented by `cameraId` .
137134 ///
138135 /// The `captureWidth` represents the width of the resulting image in pixels.
139136 /// The `captureHeight` represents the height of the resulting image in pixels.
140- CameraResolutionChangedEvent (
137+ const CameraResolutionChangedEvent (
141138 int cameraId,
142139 this .captureWidth,
143140 this .captureHeight,
@@ -146,13 +143,19 @@ class CameraResolutionChangedEvent extends CameraEvent {
146143 /// Converts the supplied [Map] to an instance of the
147144 /// [CameraResolutionChangedEvent] class.
148145 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;
152155
153156 /// Converts the [CameraResolutionChangedEvent] instance into a [Map] instance
154157 /// that can be serialized to JSON.
155- Map <String , dynamic > toJson () => {
158+ Map <String , dynamic > toJson () => < String , Object > {
156159 'cameraId' : cameraId,
157160 'captureWidth' : captureWidth,
158161 'captureHeight' : captureHeight,
@@ -162,7 +165,7 @@ class CameraResolutionChangedEvent extends CameraEvent {
162165 bool operator == (Object other) =>
163166 identical (this , other) ||
164167 other is CameraResolutionChangedEvent &&
165- super == ( other) &&
168+ super == other &&
166169 runtimeType == other.runtimeType &&
167170 captureWidth == other.captureWidth &&
168171 captureHeight == other.captureHeight;
@@ -176,58 +179,61 @@ class CameraResolutionChangedEvent extends CameraEvent {
176179class CameraClosingEvent extends CameraEvent {
177180 /// Build a CameraClosing event triggered from the camera represented by
178181 /// `cameraId` .
179- CameraClosingEvent (int cameraId) : super (cameraId);
182+ const CameraClosingEvent (int cameraId) : super (cameraId);
180183
181184 /// Converts the supplied [Map] to an instance of the [CameraClosingEvent]
182185 /// class.
183186 CameraClosingEvent .fromJson (Map <String , dynamic > json)
184- : super (json['cameraId' ]);
187+ : super (json['cameraId' ]! as int );
185188
186189 /// Converts the [CameraClosingEvent] instance into a [Map] instance that can
187190 /// be serialized to JSON.
188- Map <String , dynamic > toJson () => {
191+ Map <String , dynamic > toJson () => < String , Object > {
189192 'cameraId' : cameraId,
190193 };
191194
192195 @override
193196 bool operator == (Object other) =>
194197 identical (this , other) ||
195- super == ( other) &&
198+ super == other &&
196199 other is CameraClosingEvent &&
197200 runtimeType == other.runtimeType;
198201
199202 @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
200206 int get hashCode => super .hashCode;
201207}
202208
203209/// An event fired when an error occured while operating the camera.
204210class CameraErrorEvent extends CameraEvent {
205- /// Description of the error.
206- final String description;
207-
208211 /// Build a CameraError event triggered from the camera represented by
209212 /// `cameraId` .
210213 ///
211214 /// 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);
213216
214217 /// Converts the supplied [Map] to an instance of the [CameraErrorEvent]
215218 /// class.
216219 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;
219225
220226 /// Converts the [CameraErrorEvent] instance into a [Map] instance that can be
221227 /// serialized to JSON.
222- Map <String , dynamic > toJson () => {
228+ Map <String , dynamic > toJson () => < String , Object > {
223229 'cameraId' : cameraId,
224230 'description' : description,
225231 };
226232
227233 @override
228234 bool operator == (Object other) =>
229235 identical (this , other) ||
230- super == ( other) &&
236+ super == other &&
231237 other is CameraErrorEvent &&
232238 runtimeType == other.runtimeType &&
233239 description == other.description;
@@ -238,32 +244,32 @@ class CameraErrorEvent extends CameraEvent {
238244
239245/// An event fired when a video has finished recording.
240246class 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-
247247 /// Build a VideoRecordedEvent triggered from the camera with the `cameraId` .
248248 ///
249249 /// The `file` represents the file of the video.
250250 /// The `maxVideoDuration` shows if a maxVideoDuration shows if a maximum
251251 /// video duration was set.
252- VideoRecordedEvent (int cameraId, this .file, this .maxVideoDuration)
252+ const VideoRecordedEvent (int cameraId, this .file, this .maxVideoDuration)
253253 : super (cameraId);
254254
255255 /// Converts the supplied [Map] to an instance of the [VideoRecordedEvent]
256256 /// class.
257257 VideoRecordedEvent .fromJson (Map <String , dynamic > json)
258- : file = XFile (json['path' ]),
258+ : file = XFile (json['path' ]! as String ),
259259 maxVideoDuration = json['maxVideoDuration' ] != null
260260 ? Duration (milliseconds: json['maxVideoDuration' ] as int )
261261 : 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;
263269
264270 /// Converts the [VideoRecordedEvent] instance into a [Map] instance that can be
265271 /// serialized to JSON.
266- Map <String , dynamic > toJson () => {
272+ Map <String , dynamic > toJson () => < String , Object ? > {
267273 'cameraId' : cameraId,
268274 'path' : file.path,
269275 'maxVideoDuration' : maxVideoDuration? .inMilliseconds
0 commit comments