如何在扫描条形码时返回不同数值?

huangapple go评论95阅读模式
英文:

How to Return Different Values When Scanning a Barcode?

问题

我遇到一个问题,扫描条形码时返回相同的值到两个不同的文本小部件。我需要每个文本小部件获取不同的值。如何实现这一点?

 String? scanResult;

 Future scanBarcode() async {
    String scanResult;
    try{

    scanResult = await FlutterBarcodeScanner.scanBarcode("#ff6666", "Cancel", true, ScanMode.BARCODE);
    } on PlatformException{
      scanResult = 'Failed to get platform version';
    }
    if (!mounted) return;
    setState(() => this.scanResult = scanResult);
  } 

用于显示扫描结果的代码

  Text(
                    '$scanResult',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ),

 Text(
                    '$scanResult',
                    textAlign: TextAlign center,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ), 

我的问题: 如图所示。数据部分有两个值。如何在两个小部件中分别显示这两个数据?

英文:

I am having an issue with returning two different values when scanning a barcode. Currently, when I scan the barcode, the same value is returned for both Text widgets. I need each Text widget to take a different value. How can I achieve this?

Screenshot: Barcode Scan

如何在扫描条形码时返回不同数值?

 String? scanResult;

 Future scanBarcode() async {
    String scanResult;
    try{

    scanResult = await FlutterBarcodeScanner.scanBarcode("#ff6666", "Cancel", true, ScanMode.BARCODE);
    } on PlatformException{
      scanResult = 'Failed to get platform verion';
    }
    if (!mounted) return;
    setState(() => this.scanResult = scanResult);
  } 

My Code for show Scan-Results

  Text(
                    '$scanResult',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ),

 Text(
                    '$scanResult',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ), 

My Question : As shown in the attached image. I have two values in the data section.
How to display both data separately in two widgets?

答案1

得分: 0

you must capture and separate the result using split

Text( '${scanResult.split('\n')[0]}', textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontSize: 24.0, fontWeight: FontWeight.bold, ), ),

Text( '${scanResult.split('\n')[1]}', textAlign: TextAlign center, style: TextStyle( color: Colors.black, fontSize: 24.0, fontWeight: FontWeight.bold, ), ),

英文:

you must capture and separate the result using split

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Text(
                    &#39;${scanResult.split(&#39;\n&#39;)[0]}&#39;,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ),

 Text(
                    &#39;${scanResult.split(&#39;\n&#39;)[1]}&#39;,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ),`

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 22:07:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定