|
@@ -10,11 +10,11 @@ import 'package:r_scan/r_scan.dart';
|
|
|
const _scanType = 'com.rhyme_lph/r_scan_camera';
|
|
|
final MethodChannel _channel = const MethodChannel('$_scanType/method');
|
|
|
|
|
|
-Future<List<RScanCameraDescription>> availableRScanCameras() async {
|
|
|
+Future<List<RScanCameraDescription>?> availableRScanCameras() async {
|
|
|
try {
|
|
|
- final List<Map<dynamic, dynamic>> cameras = await _channel
|
|
|
- .invokeListMethod<Map<dynamic, dynamic>>('availableCameras');
|
|
|
- return cameras.map((Map<dynamic, dynamic> camera) {
|
|
|
+ final List<Map<dynamic, dynamic>>? cameras =
|
|
|
+ await _channel.invokeListMethod<Map<dynamic, dynamic>>('availableCameras');
|
|
|
+ return cameras?.map((camera) {
|
|
|
return RScanCameraDescription(
|
|
|
name: camera['name'],
|
|
|
lensDirection: _parseCameraLensDirection(camera['lensFacing']),
|
|
@@ -28,14 +28,16 @@ Future<List<RScanCameraDescription>> availableRScanCameras() async {
|
|
|
class RScanCameraController extends ValueNotifier<RScanCameraValue> {
|
|
|
final RScanCameraDescription description;
|
|
|
final RScanCameraResolutionPreset resolutionPreset;
|
|
|
- RScanResult result; // qr code result
|
|
|
- int _textureId; // init finish will return id
|
|
|
+ RScanResult? result; // qr code result
|
|
|
+ late int _textureId; // init finish will return id
|
|
|
bool _isDisposed = false; // when the widget dispose will set true
|
|
|
- Completer<void> _creatingCompleter; // when the camera create finish
|
|
|
- StreamSubscription<dynamic> _resultSubscription; //the result subscription
|
|
|
+ late Completer<void> _creatingCompleter; // when the camera create finish
|
|
|
+ StreamSubscription<dynamic>? _resultSubscription; //the result subscription
|
|
|
|
|
|
- RScanCameraController(this.description, this.resolutionPreset)
|
|
|
- : super(const RScanCameraValue.uninitialized());
|
|
|
+ RScanCameraController(
|
|
|
+ this.description,
|
|
|
+ this.resolutionPreset,
|
|
|
+ ) : super(const RScanCameraValue.uninitialized());
|
|
|
|
|
|
Future<void> initialize() async {
|
|
|
if (_isDisposed) return Future<void>.value();
|
|
@@ -43,19 +45,17 @@ class RScanCameraController extends ValueNotifier<RScanCameraValue> {
|
|
|
_creatingCompleter = Completer<void>();
|
|
|
|
|
|
try {
|
|
|
- final Map<String, dynamic> reply =
|
|
|
- await _channel.invokeMapMethod('initialize', <String, dynamic>{
|
|
|
+ final Map<String, dynamic> reply = (await _channel.invokeMapMethod('initialize', <String, dynamic>{
|
|
|
'cameraName': description.name,
|
|
|
'resolutionPreset': _serializeResolutionPreset(resolutionPreset),
|
|
|
- });
|
|
|
+ }))!;
|
|
|
_textureId = reply['textureId'];
|
|
|
value = value.copyWith(
|
|
|
- isInitialized: true,
|
|
|
- previewSize: Size(reply['previewWidth'].toDouble(),
|
|
|
- reply['previewHeight'].toDouble()));
|
|
|
- _resultSubscription = EventChannel('${_scanType}_$_textureId/event')
|
|
|
- .receiveBroadcastStream()
|
|
|
- .listen(_handleResult);
|
|
|
+ isInitialized: true,
|
|
|
+ previewSize: Size(reply['previewWidth'].toDouble(), reply['previewHeight'].toDouble()),
|
|
|
+ );
|
|
|
+ _resultSubscription =
|
|
|
+ EventChannel('${_scanType}_$_textureId/event').receiveBroadcastStream().listen(_handleResult);
|
|
|
} on PlatformException catch (e) {
|
|
|
//当发生权限问题的异常时会抛出
|
|
|
throw RScanCameraException(e.code, e.message);
|
|
@@ -86,8 +86,7 @@ class RScanCameraController extends ValueNotifier<RScanCameraValue> {
|
|
|
/// [isOpen] if false will close flash mode.
|
|
|
///
|
|
|
/// It will return is success.
|
|
|
- Future<bool> setFlashMode(bool isOpen) async =>
|
|
|
- await _channel.invokeMethod('setFlashMode', {
|
|
|
+ Future<bool> setFlashMode(bool isOpen) async => await _channel.invokeMethod('setFlashMode', {
|
|
|
'isOpen': isOpen,
|
|
|
});
|
|
|
|
|
@@ -96,14 +95,12 @@ class RScanCameraController extends ValueNotifier<RScanCameraValue> {
|
|
|
/// [isOpen] if false will close flash mode.
|
|
|
///
|
|
|
/// It will return is success.
|
|
|
- Future<bool> getFlashMode() async =>
|
|
|
- await _channel.invokeMethod('getFlashMode');
|
|
|
+ Future<bool> getFlashMode() async => await _channel.invokeMethod('getFlashMode');
|
|
|
|
|
|
/// flash auto open when brightness value less then 600.
|
|
|
///
|
|
|
/// [isAuto] auto
|
|
|
- Future<bool> setAutoFlashMode(bool isAuto) async =>
|
|
|
- await _channel.invokeMethod('setAutoFlashMode', {
|
|
|
+ Future<bool> setAutoFlashMode(bool isAuto) async => await _channel.invokeMethod('setAutoFlashMode', {
|
|
|
'isAuto': isAuto,
|
|
|
});
|
|
|
|
|
@@ -127,25 +124,25 @@ class RScanCameraController extends ValueNotifier<RScanCameraValue> {
|
|
|
/// camera value info
|
|
|
class RScanCameraValue {
|
|
|
final bool isInitialized;
|
|
|
- final String errorDescription;
|
|
|
- final Size previewSize;
|
|
|
+ final String? errorDescription;
|
|
|
+ final Size? previewSize;
|
|
|
|
|
|
- const RScanCameraValue(
|
|
|
- {this.isInitialized, this.errorDescription, this.previewSize});
|
|
|
+ const RScanCameraValue({
|
|
|
+ required this.isInitialized,
|
|
|
+ this.errorDescription,
|
|
|
+ this.previewSize,
|
|
|
+ });
|
|
|
|
|
|
- const RScanCameraValue.uninitialized()
|
|
|
- : this(
|
|
|
- isInitialized: false,
|
|
|
- );
|
|
|
+ const RScanCameraValue.uninitialized() : this(isInitialized: false);
|
|
|
|
|
|
- double get aspectRatio => previewSize.height / previewSize.width;
|
|
|
+ double get aspectRatio => previewSize!.height / previewSize!.width;
|
|
|
|
|
|
bool get hasError => errorDescription != null;
|
|
|
|
|
|
RScanCameraValue copyWith({
|
|
|
- bool isInitialized,
|
|
|
- String errorDescription,
|
|
|
- Size previewSize,
|
|
|
+ bool? isInitialized,
|
|
|
+ String? errorDescription,
|
|
|
+ Size? previewSize,
|
|
|
}) {
|
|
|
return RScanCameraValue(
|
|
|
isInitialized: isInitialized ?? this.isInitialized,
|
|
@@ -166,21 +163,19 @@ class RScanCameraValue {
|
|
|
class RScanCamera extends StatelessWidget {
|
|
|
final RScanCameraController controller;
|
|
|
|
|
|
- const RScanCamera(this.controller, {Key key}) : super(key: key);
|
|
|
+ const RScanCamera(this.controller, {Key? key}) : super(key: key);
|
|
|
|
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
|
- return controller.value.isInitialized
|
|
|
- ? Texture(textureId: controller._textureId)
|
|
|
- : Container();
|
|
|
+ return controller.value.isInitialized ? Texture(textureId: controller._textureId) : Container();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// camera description
|
|
|
class RScanCameraDescription {
|
|
|
RScanCameraDescription({
|
|
|
- this.name,
|
|
|
- this.lensDirection,
|
|
|
+ required this.name,
|
|
|
+ required this.lensDirection,
|
|
|
});
|
|
|
|
|
|
final String name;
|
|
@@ -188,9 +183,7 @@ class RScanCameraDescription {
|
|
|
|
|
|
@override
|
|
|
bool operator ==(Object o) {
|
|
|
- return o is RScanCameraDescription &&
|
|
|
- o.name == name &&
|
|
|
- o.lensDirection == lensDirection;
|
|
|
+ return o is RScanCameraDescription && o.name == name && o.lensDirection == lensDirection;
|
|
|
}
|
|
|
|
|
|
@override
|
|
@@ -243,8 +236,7 @@ enum RScanCameraResolutionPreset {
|
|
|
}
|
|
|
|
|
|
/// Returns the resolution preset as a String.
|
|
|
-String _serializeResolutionPreset(
|
|
|
- RScanCameraResolutionPreset resolutionPreset) {
|
|
|
+String _serializeResolutionPreset(RScanCameraResolutionPreset resolutionPreset) {
|
|
|
switch (resolutionPreset) {
|
|
|
case RScanCameraResolutionPreset.max:
|
|
|
return 'max';
|
|
@@ -258,16 +250,20 @@ String _serializeResolutionPreset(
|
|
|
return 'medium';
|
|
|
case RScanCameraResolutionPreset.low:
|
|
|
return 'low';
|
|
|
+ default:
|
|
|
+ throw ArgumentError('Unknown ResolutionPreset value');
|
|
|
}
|
|
|
- throw ArgumentError('Unknown ResolutionPreset value');
|
|
|
}
|
|
|
|
|
|
/// exception
|
|
|
class RScanCameraException implements Exception {
|
|
|
- RScanCameraException(this.code, this.description);
|
|
|
+ RScanCameraException(
|
|
|
+ this.code,
|
|
|
+ this.description,
|
|
|
+ );
|
|
|
|
|
|
String code;
|
|
|
- String description;
|
|
|
+ String? description;
|
|
|
|
|
|
@override
|
|
|
String toString() => '$runtimeType($code, $description)';
|