Navaron Bracke

provide the current torch mode instead of `hasTorch` when the camera starts

@@ -246,7 +246,9 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -246,7 +246,9 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
246 _textureId = textureId; 246 _textureId = textureId;
247 247
248 final int? numberOfCameras = startResult['numberOfCameras'] as int?; 248 final int? numberOfCameras = startResult['numberOfCameras'] as int?;
249 - final bool hasTorch = startResult['torchable'] as bool? ?? false; 249 + final TorchState currentTorchState = TorchState.fromRawValue(
  250 + startResult['currentTorchState'] as int? ?? -1,
  251 + );
250 252
251 final Map<Object?, Object?>? sizeInfo = 253 final Map<Object?, Object?>? sizeInfo =
252 startResult['size'] as Map<Object?, Object?>?; 254 startResult['size'] as Map<Object?, Object?>?;
@@ -262,7 +264,7 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -262,7 +264,7 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
262 } 264 }
263 265
264 return MobileScannerViewAttributes( 266 return MobileScannerViewAttributes(
265 - hasTorch: hasTorch, 267 + currentTorchMode: currentTorchState,
266 numberOfCameras: numberOfCameras, 268 numberOfCameras: numberOfCameras,
267 size: size, 269 size: size,
268 ); 270 );
@@ -281,9 +281,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { @@ -281,9 +281,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
281 isInitialized: true, 281 isInitialized: true,
282 isRunning: true, 282 isRunning: true,
283 size: viewAttributes.size, 283 size: viewAttributes.size,
284 - // If the device has a flashlight, let the platform update the torch state.  
285 - // If it does not have one, provide the unavailable state directly.  
286 - torchState: viewAttributes.hasTorch ? null : TorchState.unavailable, 284 + // Provide the current torch state.
  285 + // Updates are provided by the `torchStateStream`.
  286 + torchState: viewAttributes.currentTorchMode,
287 ); 287 );
288 } 288 }
289 } on MobileScannerException catch (error) { 289 } on MobileScannerException catch (error) {
1 import 'dart:ui'; 1 import 'dart:ui';
2 2
  3 +import 'package:mobile_scanner/src/enums/torch_state.dart';
  4 +
3 /// This class defines the attributes for the mobile scanner view. 5 /// This class defines the attributes for the mobile scanner view.
4 class MobileScannerViewAttributes { 6 class MobileScannerViewAttributes {
5 const MobileScannerViewAttributes({ 7 const MobileScannerViewAttributes({
6 - required this.hasTorch, 8 + required this.currentTorchMode,
7 this.numberOfCameras, 9 this.numberOfCameras,
8 required this.size, 10 required this.size,
9 }); 11 });
10 12
11 - /// Whether the current active camera has a torch.  
12 - final bool hasTorch; 13 + /// The current torch state of the active camera.
  14 + final TorchState currentTorchMode;
13 15
14 /// The number of available cameras. 16 /// The number of available cameras.
15 final int? numberOfCameras; 17 final int? numberOfCameras;
@@ -346,7 +346,9 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -346,7 +346,9 @@ class MobileScannerWeb extends MobileScannerPlatform {
346 } 346 }
347 347
348 return MobileScannerViewAttributes( 348 return MobileScannerViewAttributes(
349 - hasTorch: hasTorch, 349 + // The torch of a media stream is not available for video tracks.
  350 + // See https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints#instance_properties_of_video_tracks
  351 + currentTorchMode: TorchState.unavailable,
350 size: _barcodeReader?.videoSize ?? Size.zero, 352 size: _barcodeReader?.videoSize ?? Size.zero,
351 ); 353 );
352 } catch (error, stackTrace) { 354 } catch (error, stackTrace) {