Flutter 颤振:如何将条形码扫描值传递到TextFormField

Flutter 颤振:如何将条形码扫描值传递到TextFormField,flutter,barcode,Flutter,Barcode,我想将条形码扫描值传递到TextFrom字段中的序列号中。 有什么办法吗? 请帮我把它修好 class Scan extends StatefulWidget { @override _ScanState createState() => _ScanState(); } class _ScanState extends State<Scan> { String _data = ""; _scan() async { await F

我想将条形码扫描值传递到TextFrom字段中的序列号中。 有什么办法吗? 请帮我把它修好

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),
                  ],
                ),
                
                
类扫描扩展StatefulWidget{
@凌驾
_ScanState createState()=>\u ScanState();
}
类_ScanState扩展了状态{
字符串_data=“”;
_扫描()异步{
等待条形码扫描仪。扫描条形码(
“#000000”,“取消”,true,扫描模式。条形码)
.然后((值)=>setState(()=>_data=value));
}
final _formKey=GlobalKey();
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:appBar(
标题:文本(“维护日志”),
),
正文:卡片(
孩子:填充(
填充:边缘设置。全部(8.0),
孩子:表格(
键:_formKey,
子:列(
儿童:[
划船(
儿童:[
文本(_数据),
容器(
宽度:200.0,
子项:TextFormField(
装饰:输入装饰(hintText:“序列号”),
验证器:(val){
如果(val.length==0){
返回“需要序列号”;
}否则{
返回null;
}
},
),
),
垫片(),
升起的按钮(
子项:文本(“扫描条形码”),
按下时:()=>\u scan(),
),
垫片(挠性:2),
],
),
谢谢你的帮助。:D
另外,我是一名开始将应用程序作为项目编写的学生。这是我的第一个应用程序。

添加TextFormFieldController, 将控制器添加到textformfield。 那好吧

_scan() async {
    await FlutterBarcodeScanner.scanBarcode(
            '#000000', 'Cancel', true, ScanMode.BARCODE)
        .then((value) => setState(() => textFormFieldController.text = value));
  }
这样就可以了:)