This guide will help you migrate your ai_barcode_scanner implementation from the older versions to the new, refactored version. The new version introduces a much cleaner, more robust, and more customizable API.
The primary change is the consolidation of numerous individual styling parameters into a single, powerful configuration object: ScannerOverlayConfig.
- Overlay Configuration: Almost all direct overlay parameters (
borderColor,borderWidth,overlayColor,borderRadius,borderLength, etc.) have been removed from theAiBarcodeScannerconstructor. You now pass a singleScannerOverlayConfigobject to theoverlayConfigparameter. - Draggable Sheet Removed: The built-in
DraggableSheethas been removed in favor of the more flexiblebottomSheetBuilder. This allows you to use any widget (includingDraggableScrollableSheet) as a bottom sheet. - Gallery Button: The
hideGalleryButtonandhideGalleryIconparameters have been replaced by a singlegalleryButtonTypeenum (GalleryButtonType.filledorGalleryButtonType.icon). To hide the gallery button completely, don't implement theonImagePickcallback (though this is not a direct feature). - Pinch-to-Zoom: This feature is now enabled by default and does not require a parameter.
- Transient Feedback Color: A new parameter
colorTransitionDurationcontrols how long the success/error colors are displayed.
The biggest change is how you style the overlay.
Old Code:
AiBarcodeScanner(
borderColor: Colors.amber,
borderWidth: 8,
borderRadius: 20,
borderLength: 40,
overlayColor: Colors.black.withOpacity(0.6),
cutOutSize: 280,
successColor: Colors.greenAccent,
errorColor: Colors.redAccent,
//...
)New Code:
You now group these properties into a ScannerOverlayConfig object.
import 'package:ai_barcode_scanner/ai_barcode_scanner.dart';
AiBarcodeScanner(
overlayConfig: const ScannerOverlayConfig(
borderColor: Colors.amber,
// Note: borderWidth is now part of the painter and not directly configurable
// from the config. It has a fixed, well-proportioned value.
borderRadius: 20,
cornerLength: 40,
backgroundBlurColor: Colors.black54, // Replaces overlayColor
successColor: Colors.greenAccent,
errorColor: Colors.redAccent,
// New Options!
scannerAnimation: ScannerAnimation.center, // or .fullWidth
scannerBorder: ScannerBorder.corner, // or .full
),
//...
)If you were using sheetTitle or sheetChild, you now need to provide your own bottom sheet widget via bottomSheetBuilder.
Old Code:
AiBarcodeScanner(
sheetTitle: "My Custom Title",
sheetChild: MyCustomWidget(),
)New Code:
Recreate the draggable sheet (or any other widget) using bottomSheetBuilder.
AiBarcodeScanner(
bottomSheetBuilder: (context, controller) {
return DraggableScrollableSheet(
initialChildSize: 0.1,
minChildSize: 0.1,
maxChildSize: 0.4,
builder: (context, scrollController) {
return SingleChildScrollView(
controller: scrollController,
child: Column(
children: [
// Your custom drag handler, title, and child here
Text("My Custom Title"),
const Divider(),
MyCustomWidget(),
],
),
);
},
);
},
)The boolean flags hideGalleryButton and hideGalleryIcon are gone. Control the button's appearance with galleryButtonType.
Old Code:
// To show icon in AppBar
AiBarcodeScanner(
hideGalleryIcon: false,
hideGalleryButton: true,
)
// To show button at bottom
AiBarcodeScanner(
hideGalleryIcon: true,
hideGalleryButton: false,
)New Code:
// To show icon in AppBar
AiBarcodeScanner(
galleryButtonType: GalleryButtonType.icon,
)
// To show button at bottom (this is the default)
AiBarcodeScanner(
galleryButtonType: GalleryButtonType.filled,
)| Old Parameter | New Parameter / How to achieve | Notes |
|---|---|---|
borderColor |
overlayConfig.borderColor |
Moved into ScannerOverlayConfig. |
borderWidth |
(Removed) | The border width is now fixed for a cleaner look. |
borderRadius |
overlayConfig.borderRadius |
Moved into ScannerOverlayConfig. |
borderLength |
overlayConfig.cornerLength |
Renamed and moved into ScannerOverlayConfig. |
overlayColor |
overlayConfig.backgroundBlurColor |
Moved and renamed for clarity. Now uses a blur effect. |
cutOutWidth |
(Removed) | The scan window is now responsive by default. Use scanWindow for custom size. |
cutOutHeight |
(Removed) | The scan window is now responsive by default. Use scanWindow for custom size. |
cutOutSize |
(Removed) | Use the scanWindow parameter with a Rect for full control. |
cutOutBottomOffset |
(Removed) | The scan window is now centered by default. Use scanWindow for custom positioning. |
showError |
overlayConfig.animateOnError |
Moved into ScannerOverlayConfig. |
showSuccess |
overlayConfig.animateOnSuccess |
Moved into ScannerOverlayConfig. |
successColor |
overlayConfig.successColor |
Moved into ScannerOverlayConfig. |
errorColor |
overlayConfig.errorColor |
Moved into ScannerOverlayConfig. |
sheetTitle |
Use bottomSheetBuilder |
Replaced by a more flexible builder. |
sheetChild |
Use bottomSheetBuilder |
Replaced by a more flexible builder. |
hideSheetDragHandler |
Use bottomSheetBuilder |
Your custom bottom sheet now controls its own UI. |
hideSheetTitle |
Use bottomSheetBuilder |
Your custom bottom sheet now controls its own UI. |
hideGalleryButton |
Use galleryButtonType |
Replaced by the galleryButtonType enum. |
hideGalleryIcon |
Use galleryButtonType |
Replaced by the galleryButtonType enum. |
The rest of the parameters like onDetect, controller, validator, and onDispose remain largely the same and should work as before. This refactoring has simplified the API while increasing its power and customizability.