Skip to content

Commit efa779d

Browse files
committed
Added validator property, Deprecated validateText and validateType
1 parent fc53c2a commit efa779d

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

example/lib/main.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ class _MyHomePageState extends State<MyHomePage> {
4242
await Navigator.of(context).push(
4343
MaterialPageRoute(
4444
builder: (context) => AiBarcodeScanner(
45-
validateText: 'https://', // link to be validated
46-
validateType: ValidateType.startsWith,
45+
validator: (value) {
46+
return value.startsWith('https://');
47+
},
4748
canPop: false,
4849
onScan: (String value) {
4950
debugPrint(value);

lib/src/ai_barcode_scanner.dart

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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,7 +124,7 @@ 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

125130
/// if set barcodes will only be scanned if they fall within this [Rect]
@@ -138,8 +143,7 @@ class AiBarcodeScanner extends StatefulWidget {
138143
const AiBarcodeScanner({
139144
Key? key,
140145
required this.onScan,
141-
this.validateText,
142-
this.validateType,
146+
this.validator,
143147
this.allowDuplicates = false,
144148
this.fit = BoxFit.cover,
145149
this.controller,
@@ -172,6 +176,8 @@ class AiBarcodeScanner extends StatefulWidget {
172176
this.scanWindow,
173177
this.startDelay,
174178
this.hintWidget,
179+
@Deprecated('Use [validator] instead.') this.validateText,
180+
@Deprecated('Use [validator] instead.') this.validateType,
175181
}) : assert(validateText == null || validateType != null),
176182
assert(validateText != null || validateType == null),
177183
super(key: key);
@@ -243,29 +249,33 @@ class _AiBarcodeScannerState extends State<AiBarcodeScanner> {
243249
key: widget.key,
244250
onDetect: (BarcodeCapture barcode) async {
245251
widget.onDetect?.call(barcode);
252+
246253
if (barcode.barcodes.isEmpty) {
247254
debugPrint('Failed to scan Barcode');
248255
return;
249256
}
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-
}
257+
258+
final String code = barcode.barcodes.first.rawValue ?? "";
259+
260+
if ((widget.validator != null && !widget.validator!(code)) ||
261+
(widget.validateText?.isNotEmpty == true &&
262+
!widget.validateType!.toValidateTypeBool(
263+
code,
264+
widget.validateText!,
265+
))) {
266+
HapticFeedback.heavyImpact();
267+
debugPrint('Invalid Barcode => $code');
268+
_isSuccess = false;
269+
setState(() {});
270+
return;
262271
}
272+
263273
_isSuccess = true;
264274
HapticFeedback.lightImpact();
265-
final String code = barcode.barcodes.first.rawValue!;
266275
debugPrint('Barcode rawValue => $code');
267276
widget.onScan(code);
268277
setState(() {});
278+
269279
if (widget.canPop && mounted && Navigator.canPop(context)) {
270280
Navigator.pop(context);
271281
}

0 commit comments

Comments
 (0)