【问题标题】:Is there a way to display the barcode value in a text field in Flutter?有没有办法在 Flutter 的文本字段中显示条形码值?
【发布时间】: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');
}}

【问题讨论】:

    标签: flutter barcode-scanner


    【解决方案1】:

    textController.text = barcode

    而不是

    barcode = textController.text; 
    

    在凸起按钮中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多