简体   繁体   English

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

[英]Flutter: How to Pass Barcode Scan value into TextFormField

I want to pass the Barcode scan value into the Serial Number that in the TextFromField.我想将条形码扫描值传递到 TextFromField 中的序列号。 Is there any way to do it?有什么办法吗? Please help me to fix it out.请帮我解决它。

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),
                  ],
                ),
                
                

Thank you for your help.谢谢您的帮助。 :D PS I am a student who is starting to write apps as a project. :D PS 我是一名开始将应用程序作为项目编写的学生。 This is my first app.这是我的第一个应用程序。

Add a TextFormFieldController, add the controller to the textformfield.添加一个 TextFormFieldController,将 controller 添加到 textformfield。 then booom!!然后繁荣!

_scan() async {
    await FlutterBarcodeScanner.scanBarcode(
            '#000000', 'Cancel', true, ScanMode.BARCODE)
        .then((value) => setState(() => textFormFieldController.text = value));
  }

this will do it:)这样就可以了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM