How to handle external barcode scanner enter suffix added to scanned string in Flutter

flutter barcode scanner
barcode_scan 2.0 1
package barcode scanner
module barcode_scan not found
flutter textfield( controller)
flutter textfield set value
flutter textformfield onchanged
textformfield flutter

I'm new to Flutter and I'm stuck trying to handle the scan in an Android smartphone that has a physical barcode scanner in it. It scans a barcode and returns a string like "S3XAHKBC" but with a suffix "\n".

So I'm not talking about a software camera driven barcode reader.

In an Android application written in Java I would do something like:

myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT)) {
      submit();
    }
    return false;
  }
});

In Flutter I've tried everything I found in the documentation and this is what I'd like to get working:

TextFormField(
  controller: barcodeStringController,
  autofocus: true,
  textCapitalization: TextCapitalization.characters,
  style: TextStyle(fontSize: 20),
  textInputAction: TextInputAction.done,
  onFieldSubmitted: (v) => submit(),
);

Do anyone have experience with something like this?


You can use Textfield onChanged event or TextEditingController i guess .

https://flutter.dev/docs/cookbook/forms/text-field-changes

barcode_keyboard_listener, Listens keyboard input from barcode scanner. TextEditingController(); int _scanButtonKeyCode; String _scanResult = '' addAll([ Text('Press the scan button, its code will appear in the text field below'), TextField( controller: _tecScanKeyCode, decoration: InputDecoration( suffix: IconButton( icon:  External barcode scanner can be enabled/disabled. App Tap to select the required external barcode scanner app. Settings Adjust external scanner relevant options. Keyboard Behavior. The following behaviors can be defined: Keyboard Behavior Define behavior (prefix, suffix, cursor movements, ) for target-apps.


I've found a solution that works but isn't really nice

TextFormField(
  controller: barcodeStringController,
  textAlign: TextAlign.right,
  autofocus: true,
  textInputAction: TextInputAction.done,
  textCapitalization: TextCapitalization.characters,
  style: TextStyle(fontSize: 20),
  keyboardType: TextInputType.text,
  maxLines: null,
  onFieldSubmitted: (value) => submit(), //to handle submit from keyboard
  validator: (value){
    //to handle submit from hardware barcode reader
    if(value.contains('\n') || value.contains('\t')){ 
      barcodeStringController.text = value.replaceAll('\n', '').replaceAll('\t', '');
      submit();
    }
    return null;
  },
);

Barcode scanner text input, Consider adding another form of input, such as a Text input control, for users to This design makes the barcode scanner handle gracefully batch scanning scenarios. If you enter (or scan) something in TextBox 1 and then press Enter (​which Hello All,I am trying to read a USB barcode scanner configured as a keyboard  My app takes input from a 2D barcode scanner that acts as a keyboard. The scanned item is about 20 lines long and includes line-feeds. When the scan is complete, I parse through each line extracting needed data. Right now I have a TMemo component that has focus. When the barcode is scanned, the TMemo takes all the lines.


After many attempts, I finally achieved the desired effect by listening to the TextField.onChanged method.

TextField(
   keyboardType: TextInputType.multiline,
   // maxLines !=1, if maxLines==1 can't type '\n', and onChanged can't capture ‘\n’
   maxLines: null,
   autofocus: true,
   controller: state.ipController,
   focusNode: state.ipFocusNode,
   onChanged: (text) {
     print('onChanged : -$text-');
     if (text.endsWith('\n')) {
        // remove '\n'
        String filter = text.substring(0, text.length - 1);
        // update 
        state.ipController.value = TextEditingValue(
             text: filter,
             selection: TextSelection.collapsed(offset: filter.length),
        );
        /// you can do something
        ......
     }
   },
 ),

if (Platform.isAndroid) {
    return RawKeyboardListener(
      focusNode: state.keyboardFocusNode,
      onKey: (keyEvent) {
        // key down
        if (keyEvent.runtimeType.toString() == 'RawKeyDownEvent') {
          RawKeyEventDataAndroid key = keyEvent.data; 
          if (isOnScan(key.keyCode)) { 
            if (!state.ipFocusNode.hasFocus) {
              // focus
              state.ipFocusNode.requestFocus();
            }
            // clear text
            state.ipController.clear();
          }
        } else {
//         key up
        }
      },
      child: Scaffold(……),
    );
  } 

//   second method :
  RawKeyboard.instance.addListener((RawKeyEvent keyEvent) {
  if (keyEvent.runtimeType.toString() == 'RawKeyDownEvent') {
    RawKeyEventDataAndroid key = keyEvent.data;
    if (ScanUtils.isOnScan(key.keyCode)) {
      if (!state.ipFocusNode.hasFocus) {
        state.ipFocusNode.requestFocus();
      }
      state.ipController.clear();
    }
  } else { }
});

// left scan keyCode
static final int LEFT_SCAN_KEY = 132;
// right scan keyCode
static final int RIGHT_SCAN_KEY = 133;
// scan keyCode
static final int SCAN_KEY = 165;
// bluetooth scan keyCode (i'm not test)
static final int SCAN_KEY = 113;
   
static bool isOnScan(int keyCode) {
  return keyCode == LEFT_SCAN_KEY ||
      keyCode == RIGHT_SCAN_KEY ||
      keyCode == SCAN_KEY ||
      keyCode == BLUETOOTH_SCAN_KEY;
}

How to fix barcode scanner, How to remove prefixes or suffixes from the scanner output. You can also factory reset your Bluetooth barcode scanner. 2016 · Our configurable barcode scanners can be set to append output strings with the sending of function keys. A plugin for Flutter apps that adds barcode scanning support on both Android and  The soft-keyboard with an integrated barcode scanner / NFC reader provides a fast and convenient way of scanning bar-codes or reading NFC tags with any application. The scanned or read data appears in the target apps as entered manually by the user, manual typing or voice input are of course still supported.


Flutter hardware barcode scanner, One day hands-on workshop using Flutter SDK for building Android/iOS apps Created and designed various external hardware devices that integrate with This design makes the barcode scanner handle gracefully batch scanning scenarios. It scans a barcode and returns a string like "S3XAHKBC" but with a suffix " ". Bar code scanners can be programmed to send a prefix and suffix for each scan. Many scanners allow you to program them with a verity of characters. For widest compatibilty STX (start of text, 0x02 ASCII) and ETX (end of text, 0x03 ASCII) are the best choices. I created a KeyDown event in a WPF app and scanned a bar code. The first key I


Symbol barcode scanner enter after scan, Enter a string using the Alphanumeric Keyboard on page Scan End of Message on page 90. Enter all fields that should appear when scanning your QR code. 4G Wireless Barcode Scanner Share on: Support wireless and USB cable based If you want a carriage return after the barcode, scan the Add CR Suffix barcode. If you have suffix in your barcode (or some other suffix you desire), you can remove need for tapping onto Enter key; otherwise, once you scan into field, you tap on Enter key and there you are, your custom action can be triggered after scan so easily 13.


Flutter Bluetooth External Barcode Scanner, I need to use an external barcode scanner which is connected via Bluetooth to me I originally thought the text was part of the Key Event, but the String is not there. component and scan barcode, nothing you get empty result, with enter key,  When the Auto Reconnect On bar code is scanned, the scanner begins the relink process immediately, without user intervention. Page 67: Relink Time-Out Scan the Maximum Link Attempts bar code, then scan the number of attempts for the setting (from 0-100) from the inside back cover. Scan Save to save the setting. Default = 0.