1

I'm facing an issue with barcode scanning in Flutter when using a Zebra handheld scanner. The barcode scanning functionality only works when I close the page and reopen it. I've provided the code below for reference:

StockLookupScreen:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../shared_widgets/scanWidget.dart';
import '../shared_widgets/navbar.dart';
import 'stockLookIpResults.dart';

class StockLookupScreen extends StatelessWidget {
  final String title;

  const StockLookupScreen({Key? key, required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: "Stock LookUp",
        showSettingsIcon: false,
        showLogoutIcon: true,
        showBackButton: true,
        onBackButtonPressed: () {
          Get.back();
        },
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Padding(
              padding: const EdgeInsets.all(5.0),
              child: BarcodeScannerField(
                onSubmitted: (searchText) {
                  // Handle search functionality
                  print('Search text: $searchText');
                  Get.to(() => StockLookupResults(searchText));
                },
              ),
            ),
            // Add other widgets as needed
          ],
        ),
      ),
    );
  }
}

BarcodeScannerField:

import 'package:code_scan_listener/code_scan_listener.dart';
import 'package:flutter/material.dart';

class BarcodeScannerField extends StatefulWidget {
  final Function(String) onSubmitted;

  BarcodeScannerField({required this.onSubmitted});

  @override
  _BarcodeScannerFieldState createState() => _BarcodeScannerFieldState();
}

class _BarcodeScannerFieldState extends State<BarcodeScannerField> {
  String? _barcode;
  TextEditingController _textFieldController = TextEditingController();
  bool _isReadOnly = true;

  void toggleReadOnly() {
    setState(() {
      _isReadOnly = !_isReadOnly;
    });
  }

  void handleSubmit() {
    setState(() {
      _barcode = _textFieldController.text;
      widget.onSubmitted(_barcode ?? ''); // Trigger the callback
    });
  }

  @override
  void initState() {
    super.initState();
    _textFieldController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    Color textFieldBackgroundColor = _isReadOnly ? const Color(0xFFF7F6F6) : Colors.white;

    return Center(
      child: CodeScanListener(
        bufferDuration: const Duration(milliseconds: 200),
        onBarcodeScanned: (barcode) {
          if (barcode.isNotEmpty) {
            debugPrint(barcode);

            // Remove special characters
            String cleanedBarcode = barcode.replaceAll(RegExp(r'[\]\[]'), '');

            setState(() {
              _barcode = cleanedBarcode;
              _textFieldController.text = cleanedBarcode; // Update TextField with cleaned barcode
            });
          }
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                height: 45,
                decoration: BoxDecoration(
                  color: textFieldBackgroundColor,
                  borderRadius: BorderRadius.circular(2),
                  border: Border.all(
                    color: const Color(0xFFAAAAAA),
                    width: 0.75,
                  ),
                ),
                child: TextField(
                  controller: _textFieldController,
                  readOnly: _isReadOnly,
                  style: const TextStyle(
                    fontFamily: 'Roboto',
                    fontSize: 14,
                    color: Color(0xFFAAAAAA),
                    height: 1.3,
                  ),
                  decoration: InputDecoration(
                    contentPadding: const EdgeInsets.all(8),
                    border: InputBorder.none,
                    suffixIcon: Container(
                      width: 30,
                      height: 45,
                      decoration: BoxDecoration(
                        color: const Color(0xFFAAAAAA),
                        borderRadius: const BorderRadius.only(
                          topRight: Radius.circular(2),
                          bottomRight: Radius.circular(2),
                        ),
                        border: Border.all(
                          color: const Color(0xFFAAAAAA),
                          width: 0.75,
                        ),
                      ),
                      child: IconButton(
                        icon: _isReadOnly
                            ? const Icon(
                                Icons.edit,
                                color: const Color(0xFF343A40),
                                size: 24,
                              )
                            : const Icon(
                                Icons.search,
                                color: const Color(0xFF343A40),
                                size: 24,
                              ),
                        onPressed: _isReadOnly ? toggleReadOnly : handleSubmit,
                      ),
                    ),
                  ),
                ),
              ),
            ),
            const Padding(
              padding: EdgeInsets.all(20.0),
            ),
          ],
        ),
      ),
    );
  }
}

I would appreciate any guidance or suggestions on how to resolve this issue. Thank you in advance!

more info: this code loads initial (visual it looks right) but the BarcodeScannerField widget does not work on the zebra mc3300 scanner, I put a lot of brake points into the code and it seems the BarcodeScannerField widget is nerve called when the scanner is used unless i close the app and reopen it then it works and hit the brake points.it only behaves like this on the zebra scanner not on the cipherlabs scanner (K95).

Jim Lu
  • 11
  • 3

0 Answers0