@@ -19,12 +19,17 @@ class AiBarcodeScanner extends StatefulWidget {
1919
2020 /// Validate barcode text with [ValidateType]
2121 /// [validateText] and [validateType] must be set together.
22+ @Deprecated ('Use [validator] instead.' )
2223 final String ? validateText;
2324
2425 /// Validate type [ValidateType]
2526 /// Validator working with single string value only.
27+ @Deprecated ('Use [validator] instead.' )
2628 final ValidateType ? validateType;
2729
30+ /// Validate barcode text with a function
31+ final bool Function (String value)? validator;
32+
2833 /// Set to false if you don't want duplicate barcode to be detected
2934 final bool allowDuplicates;
3035
@@ -119,9 +124,12 @@ class AiBarcodeScanner extends StatefulWidget {
119124 /// If this is null, a black [ColoredBox] is used as placeholder.
120125 final Widget Function (BuildContext , Widget ? )? placeholderBuilder;
121126
122- ///The function that signals when the barcode scanner is started.
127+ /// The function that signals when the barcode scanner is started.
123128 final void Function (MobileScannerArguments ? )? onScannerStarted;
124129
130+ /// Called when this object is removed from the tree permanently.
131+ final void Function ()? onDispose;
132+
125133 /// if set barcodes will only be scanned if they fall within this [Rect]
126134 /// useful for having a cut-out overlay for example. these [Rect]
127135 /// coordinates are relative to the widget size, so by how much your
@@ -138,8 +146,7 @@ class AiBarcodeScanner extends StatefulWidget {
138146 const AiBarcodeScanner ({
139147 Key ? key,
140148 required this .onScan,
141- this .validateText,
142- this .validateType,
149+ this .validator,
143150 this .allowDuplicates = false ,
144151 this .fit = BoxFit .cover,
145152 this .controller,
@@ -169,9 +176,12 @@ class AiBarcodeScanner extends StatefulWidget {
169176 this .errorBuilder,
170177 this .placeholderBuilder,
171178 this .onScannerStarted,
179+ this .onDispose,
172180 this .scanWindow,
173181 this .startDelay,
174182 this .hintWidget,
183+ @Deprecated ('Use [validator] instead.' ) this .validateText,
184+ @Deprecated ('Use [validator] instead.' ) this .validateType,
175185 }) : assert (validateText == null || validateType != null ),
176186 assert (validateText != null || validateType == null ),
177187 super (key: key);
@@ -197,6 +207,10 @@ class _AiBarcodeScannerState extends State<AiBarcodeScanner> {
197207 void dispose () {
198208 controller.dispose ();
199209 super .dispose ();
210+
211+ if (widget.onDispose != null ) {
212+ widget.onDispose !();
213+ }
200214 }
201215
202216 @override
@@ -243,29 +257,33 @@ class _AiBarcodeScannerState extends State<AiBarcodeScanner> {
243257 key: widget.key,
244258 onDetect: (BarcodeCapture barcode) async {
245259 widget.onDetect? .call (barcode);
260+
246261 if (barcode.barcodes.isEmpty) {
247262 debugPrint ('Failed to scan Barcode' );
248263 return ;
249264 }
250- if (widget.validateText? .isNotEmpty == true ) {
251- if (! widget.validateType! .toValidateTypeBool (
252- barcode.barcodes.first.rawValue ?? "" ,
253- widget.validateText! ,
254- )) {
255- HapticFeedback .heavyImpact ();
256- final String code = barcode.barcodes.first.rawValue! ;
257- debugPrint ('Invalid Barcode => $code ' );
258- _isSuccess = false ;
259- setState (() {});
260- return ;
261- }
265+
266+ final String code = barcode.barcodes.first.rawValue ?? "" ;
267+
268+ if ((widget.validator != null && ! widget.validator !(code)) ||
269+ (widget.validateText? .isNotEmpty == true &&
270+ ! widget.validateType! .toValidateTypeBool (
271+ code,
272+ widget.validateText! ,
273+ ))) {
274+ HapticFeedback .heavyImpact ();
275+ debugPrint ('Invalid Barcode => $code ' );
276+ _isSuccess = false ;
277+ setState (() {});
278+ return ;
262279 }
280+
263281 _isSuccess = true ;
264282 HapticFeedback .lightImpact ();
265- final String code = barcode.barcodes.first.rawValue! ;
266283 debugPrint ('Barcode rawValue => $code ' );
267284 widget.onScan (code);
268285 setState (() {});
286+
269287 if (widget.canPop && mounted && Navigator .canPop (context)) {
270288 Navigator .pop (context);
271289 }
0 commit comments