英文:
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(
'${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,
),
),`
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论