【发布时间】:2021-06-14 10:58:47
【问题描述】:
What i want to achieve looks like this:
我在网上和 stackoverflow 上查看了多个来源,其中许多表明我们可以使用凸起按钮在文本字段中显示值。
到目前为止,我设法使用条形码扫描仪进行扫描,但扫描的条形码并没有像我想要的那样出现在文本字段中。
我的代码:
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');
}}
【问题讨论】: