问题描述
我想将条形码扫描值传递到TextFromField中的序列号中。 有什么办法吗? 请帮我解决它。
class Scan extends StatefulWidget {
@override
_ScanState createState() => _ScanState();
}
class _ScanState extends State<Scan> {
String _data = "";
_scan() async {
await FlutterBarcodeScanner.scanBarcode(
'#000000','Cancel',true,ScanMode.BARCODE)
.then((value) => setState(() => _data = value));
}
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text('Maintenance Log'),),body: Card(
child: Padding(
padding: EdgeInsets.all(8.0),child: Form(
key: _formKey,child: Column(
children: <Widget>[
Row(
children: <Widget>[
Text(_data),Container(
width: 200.0,child: TextFormField(
decoration: InputDecoration(hintText: 'Serial Number'),validator: (val) {
if (val.length == 0) {
return "S/N is Required";
} else {
return null;
}
},Spacer(),RaisedButton(
child: Text('Scan Barcode'),onPressed: () => _scan(),Spacer(flex: 2),],
感谢您的帮助。 :D 附言我是一名开始将应用程序作为项目编写的学生。这是我的第一个应用。
解决方法
添加一个TextFormFieldController, 将控制器添加到textformfield。 然后bo!
_scan() async {
await FlutterBarcodeScanner.scanBarcode(
'#000000','Cancel',true,ScanMode.BARCODE)
.then((value) => setState(() => textFormFieldController.text = value));
}
这会做到的:)