-
Notifications
You must be signed in to change notification settings - Fork 2
Features
The QrcodeBarcodeScanner
library offers a unique solution for interpreting virtual keyboard input as scanned QR codes or barcodes. Instead of using a camera to read barcodes, this library listens to keyboard inputs, making it ideal for setups where barcodes are input via a barcode scanner that emulates keyboard input (also known as keyboard wedge scanners). Below is an explanation of the key features and the theory behind its functioning.
Barcode and QR code scanners often function as "keyboard wedges," meaning that when they scan a code, they simulate typing the characters into the device just as if they were entered on a keyboard. The QrcodeBarcodeScanner
leverages this behavior by capturing the keyboard events, collecting them into a buffer, and then processing the full sequence of characters as a single barcode or QR code.
The QrcodeBarcodeScanner
uses the Flutter HardwareKeyboard
API to listen to all key events. This allows the library to capture keyboard input without needing direct access to hardware like cameras or external devices.
- Theory: The scanner captures each keystroke and processes it as part of the barcode or QR code being input. Each keypress is treated as a character, and the library waits until no new characters are input before it processes the full barcode.
HardwareKeyboard.instance.addHandler(_keyBoardCallback);
The library incorporates a DelayedActionHandler
to ensure that barcode input is processed only after a short pause in keyboard events. Typically, barcode scanners "type" the entire barcode quickly, and this delay ensures that the input is collected in full before being interpreted.
- Theory: Barcodes and QR codes are usually transmitted as a series of rapid key presses. By delaying the processing by a small interval (100ms), the library ensures that the entire code is collected before performing the action, avoiding partial input.
_actionHandler.executeDelayed(() {
final String scannedCode = _pressedKeys.isNotEmpty ? _pressedKeys.join() : "";
onScannedCallback(scannedCode.trim());
_pressedKeys.clear();
});
The library ensures that only valid characters are processed. It filters out invalid key events or empty characters, so only meaningful input (such as alphanumeric characters from the barcode or QR code) is considered.
- Theory: Not all keyboard events are relevant for barcode or QR code scanning. For example, function keys or empty input should not trigger the scanning process. By validating each character, the library avoids processing invalid input.
bool _isValidCharacter(String? character) {
return character != null &&
character.isNotEmpty &&
character.codeUnits.any((unit) => unit != 0);
}
To avoid conflicts between barcode scanning and user text input, the library can detect when a text input field (e.g., a TextField
or TextFormField
) is focused. If such a field is focused, the barcode scanning functionality is bypassed, allowing normal keyboard input to occur without interference.
- Theory: When a text input field is focused, the keyboard input is likely intended for user interaction, not for barcode scanning. The library detects these cases and prevents the input from being treated as a barcode scan.
if (_isTextInputFocused()) {
return false; // Bypass scanner logic when text input is focused
}
The main feature of the library is the onScannedCallback
, a callback function that is triggered once the full barcode or QR code is detected. After processing the keyboard input, the library sends the complete code to this callback for further action.
- Theory: Once the barcode or QR code is fully collected, it needs to be processed (e.g., displayed or stored). The callback allows the user to define what happens next with the scanned data, enabling full flexibility in handling the scanned code.
final ScannedCallback onScannedCallback;
The library provides a way to cancel any in-progress scan. This can be useful if the user needs to stop the scanning process prematurely.
- Theory: In some cases, the scan process may need to be interrupted (e.g., if the user accidentally starts typing instead of scanning). This feature ensures that the pending scan action can be safely discarded.
void cancelScan() {
_actionHandler.cancelDelayed();
_pressedKeys.clear();
}
The QrcodeBarcodeScanner
uses platform channels to provide compatibility with multiple platforms, ensuring that the same logic works across different devices.
- Theory: By using platform channels, the library is able to communicate with the underlying platform to retrieve platform-specific details (e.g., version information) and provide a seamless experience across different devices.
Future<String?> getPlatformVersion() {
return QrcodeBarcodeScannerPlatform.instance.getPlatformVersion();
}
The QrcodeBarcodeScanner
library leverages keyboard input to simulate barcode or QR code scanning, eliminating the need for camera-based scanners. The combination of delayed action processing, input validation, text input focus detection, and callback mechanisms makes it a powerful and efficient tool for developers looking to implement barcode or QR code scanning functionality with minimal overhead.
-
Home
A high-level overview of the package and its key features.
- Getting Started
-
Core Features
- Key Functionalities: A breakdown of how the library works behind the scenes and the key methods involved.
-
Examples
- Basic Scanner Setup: Simple implementation of the scanner.
- Handling Scanned Values: Example of handling and displaying scanned values.
- Managing Focus & Input: Managing focus on text inputs to avoid scanner conflicts.
-
Additional Resources
- Changelog: Stay updated on new features and fixes.
- Contributing: Guidelines for contributing to the project.
- License: Information about the project’s licensing.