Navaron Bracke

rename onDetect to onBarcodeDetected; remove typedefs; add placeholder builder; …

…fix bug with media query; replace onSTart with onScannerRestarted
@@ -6,39 +6,38 @@ import 'package:mobile_scanner/src/mobile_scanner_controller.dart'; @@ -6,39 +6,38 @@ import 'package:mobile_scanner/src/mobile_scanner_controller.dart';
6 import 'package:mobile_scanner/src/objects/barcode_capture.dart'; 6 import 'package:mobile_scanner/src/objects/barcode_capture.dart';
7 import 'package:mobile_scanner/src/objects/mobile_scanner_arguments.dart'; 7 import 'package:mobile_scanner/src/objects/mobile_scanner_arguments.dart';
8 8
9 -typedef MobileScannerCallback = void Function(BarcodeCapture barcodes);  
10 -typedef MobileScannerArgumentsCallback = void Function(  
11 - MobileScannerArguments? arguments,  
12 -);  
13 -  
14 -/// A widget showing a live camera preview. 9 +/// The [MobileScanner] widget displays a live camera preview.
15 class MobileScanner extends StatefulWidget { 10 class MobileScanner extends StatefulWidget {
16 /// The controller that manages the barcode scanner. 11 /// The controller that manages the barcode scanner.
17 final MobileScannerController controller; 12 final MobileScannerController controller;
18 13
19 - /// Function that gets called when a Barcode is detected. 14 + /// The [BoxFit] for the camera preview.
20 /// 15 ///
21 - /// [barcode] The barcode object with all information about the scanned code.  
22 - /// [startInternalArguments] Information about the state of the MobileScanner widget  
23 - final MobileScannerCallback onDetect; 16 + /// Defaults to [BoxFit.cover].
  17 + final BoxFit fit;
24 18
25 - /// Function that gets called when the scanner is started.  
26 - ///  
27 - /// [arguments] The start arguments of the scanner. This contains the size of  
28 - /// the scanner which can be used to draw a box over the scanner.  
29 - final MobileScannerArgumentsCallback? onStart; 19 + /// The function that signals when new barcodes were detected by the [controller].
  20 + final void Function(BarcodeCapture barcodes) onBarcodeDetected;
30 21
31 - /// Handles how the widget should fit the screen.  
32 - final BoxFit fit; 22 + /// The function that signals when the barcode scanner is restarted,
  23 + /// due to coming back to the foreground.
  24 + final void Function(MobileScannerArguments? arguments)? onScannerRestarted;
33 25
34 - /// Whether to automatically resume the camera when the application is resumed  
35 - final bool autoResume; 26 + /// The function that builds a placeholder widget when the scanner
  27 + /// is not yet displaying its camera preview.
  28 + ///
  29 + /// If this is null, a black [ColoredBox] is used as placeholder.
  30 + final Widget Function(BuildContext, Widget?)? placeholderBuilder;
36 31
37 - /// Create a [MobileScanner] with a [controller].  
38 - /// The [controller] must have been initialized, using [MobileScannerController.start]. 32 + /// Create a new [MobileScanner] using the provided [controller]
  33 + /// and [onBarcodeDetected] callback.
39 const MobileScanner({ 34 const MobileScanner({
40 required this.controller, 35 required this.controller,
41 this.fit = BoxFit.cover, 36 this.fit = BoxFit.cover,
  37 + required this.onBarcodeDetected,
  38 + this.onScannerRestarted,
  39 + this.placeholderBuilder,
  40 + super.key,
42 }); 41 });
43 42
44 @override 43 @override
@@ -100,20 +99,19 @@ class _MobileScannerState extends State<MobileScanner> @@ -100,20 +99,19 @@ class _MobileScannerState extends State<MobileScanner>
100 99
101 @override 100 @override
102 Widget build(BuildContext context) { 101 Widget build(BuildContext context) {
103 - return ValueListenableBuilder(  
104 - valueListenable: controller.startArguments, 102 + return ValueListenableBuilder<MobileScannerArguments?>(
  103 + valueListenable: widget.controller.startArguments,
105 builder: (context, value, child) { 104 builder: (context, value, child) {
106 - value = value as MobileScannerArguments?;  
107 if (value == null) { 105 if (value == null) {
108 - return const ColoredBox(color: Colors.black);  
109 - } else {  
110 - controller.barcodes.listen((barcode) {  
111 - widget.onDetect(barcode);  
112 - }); 106 + return widget.placeholderBuilder?.call(context, child) ??
  107 + const ColoredBox(color: Colors.black);
  108 + }
  109 +
113 return ClipRect( 110 return ClipRect(
114 - child: SizedBox(  
115 - width: MediaQuery.of(context).size.width,  
116 - height: MediaQuery.of(context).size.height, 111 + child: LayoutBuilder(
  112 + builder: (_, constraints) {
  113 + return SizedBox.fromSize(
  114 + size: constraints.biggest,
117 child: FittedBox( 115 child: FittedBox(
118 fit: widget.fit, 116 fit: widget.fit,
119 child: SizedBox( 117 child: SizedBox(
@@ -124,9 +122,10 @@ class _MobileScannerState extends State<MobileScanner> @@ -124,9 +122,10 @@ class _MobileScannerState extends State<MobileScanner>
124 : Texture(textureId: value.textureId!), 122 : Texture(textureId: value.textureId!),
125 ), 123 ),
126 ), 124 ),
  125 + );
  126 + },
127 ), 127 ),
128 ); 128 );
129 - }  
130 }, 129 },
131 ); 130 );
132 } 131 }