有没有办法在 Flutter 的文本字段中显示条形码值?
Is there a way to display the barcode value in a text field in Flutter?
What i want to achieve looks like this:
我在网上和 Whosebug 上查看了多个来源,许多来源显示我们可以通过使用凸起按钮在文本字段中显示值。
到目前为止,我设法使用条形码扫描仪进行了扫描,但扫描的条形码并没有像我希望的那样出现在文本字段中。
我的代码:
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;
});
},
),
),
初始化:
final textController = TextEditingController();
String barcode = "";
调用扫描器的代码:
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');
}}
textController.text = barcode
而不是
barcode = textController.text;
在 RaisedButton
What i want to achieve looks like this:
我在网上和 Whosebug 上查看了多个来源,许多来源显示我们可以通过使用凸起按钮在文本字段中显示值。
到目前为止,我设法使用条形码扫描仪进行了扫描,但扫描的条形码并没有像我希望的那样出现在文本字段中。
我的代码:
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;
});
},
),
),
初始化:
final textController = TextEditingController();
String barcode = "";
调用扫描器的代码:
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');
}}
textController.text = barcode
而不是
barcode = textController.text;
在 RaisedButton