User1234
User1234

Reputation: 35

Is there a way to display the barcode value in a text field in Flutter?

What i want to achieve looks like this:

I have looked through mulitple sources online and on stackoverflow and many show that we can display the value in a textfield by using a raisedbutton.

So far i managed to use the barcode scanner to scan but the scanned barcode doesnt appear in the textfield like i want it to.

My Code:

Container(
    child: TextField(
           controller: textController,
            decoration: InputDecoration(
            hintText: 'SKU',
                  ),
                ),
              ),Container(
                 width: 80,
                height: 30,
                child: RaisedButton(
                  child: Text('SCAN'),
                  onPressed: (){
                    setState(() {
                      scan();
                      barcode = textController.text;
                    });
                 },
                ),
              ),

Initialisation:

final textController = TextEditingController();
String barcode = "";

Code to invoke scanner:

Future scan() async {
try {
  String barcode = await BarcodeScanner.scan();
  setState(() => this.barcode = barcode);
} on PlatformException catch (e) {
  if (e.code == BarcodeScanner.CameraAccessDenied) {
    setState(() {
      this.barcode = 'The user did not grant the camera permission!';
    });
  } else {
    setState(() => this.barcode = 'Unknown error: $e');
  }
} on FormatException{
  setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
  setState(() => this.barcode = 'Unknown error: $e');
}}

Upvotes: 1

Views: 1213

Answers (1)

Fatima ayaa
Fatima ayaa

Reputation: 650

textController.text = barcode

instead of

barcode = textController.text; 

in RaisedButton

Upvotes: 1

Related Questions