代码之家  ›  专栏  ›  技术社区  ›  W P

飞镖,如何在扫描时将条形码扫描值传递到textformfield并更新文本

  •  0
  • W P  · 技术社区  · 6 年前

    扫描后如何将条形码值传递到textforfield?barcode函数将条形码值传递给字符串条形码。它是一个ListTile对象,其ontap函数定义为scan();

    我想立即将该值传递回字段。我试了一个setState()函数,但没弄清楚。谢谢。

    import 'dart:async';
    import 'package:barcode_scan/barcode_scan.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:flutter/material.dart';
    import 'package:intl/intl.dart';
    import 'ItemData.dart';
    import 'homepage.dart';
    import 'Barcode.dart';
    import 'package:flutter/services.dart';
    
    
    class CreateWidget extends StatefulWidget {
      @override
      CreateState createState() => CreateState();
    
    }
    Data newData = new Data();
    
    class CreateState extends State<CreateWidget> {
    final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
    void submit() {
        _formKey.currentState.save();
    
    }
    
    
    
      String barcode = "";
    
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return Column(
        children: <Widget>[
          Flexible(
            flex: 0,
            child: Center(
              child: Form(
                key: this._formKey,
                child: Flex(
                  direction: Axis.vertical,
                  children: <Widget>[
                    ListTile(
                      title: TextFormField (
                        initialValue: '',
                        decoration: new InputDecoration(
                          icon: new Icon(Icons.info),
                          hintText: "Title",
                        ),
                        validator: (val) => val.isEmpty ? null : 'Not a valid Title',
                        onSaved: (val) => newData.title = val,
                  ),
                ),
                ListTile(
                  title: TextFormField(
                    initialValue: '',
                    decoration: new InputDecoration(
                      icon: new Icon(Icons.info),
                      hintText: "Location",
                    ),
                    validator: (val) => val.isEmpty ? 'Location is required' : null,
                    onSaved: (val) => newData.location = val,
                  ),
                ),
    

    ///////////////////////////////////// 这是我希望操作发生的列表磁贴。

                ListTile(
                  title: TextFormField(
                    initialValue: '',
                    decoration: new InputDecoration(
                      icon: new Icon(Icons.info),
                      hintText: "Barcode",
                    ),
                    validator: (val) => val.isEmpty ? 'Barcode is required' : null,
                    onSaved: (val) => newData.barcode = val,
                  ),
                  trailing: new Icon(Icons.search),
                  onTap: () {
                    {scan();}
                  },
                ),
                ListTile(
                  title: TextFormField(
                    initialValue: '',
                    decoration: new InputDecoration(
                      icon: new Icon(Icons.info),
                      hintText: "Type",
                    ),
                    validator: (val) => val.isEmpty ? 'Type is required' : null,
                    onSaved: (val) => newData.type = val,
                  ),
                ),
                ListTile(
                  leading: Icon(Icons.access_time),
                  title: Text(_getDateNow()),
                ),
                RaisedButton(
                  color: Colors.red,
                  textColor: Colors.white,
                  child: new Text('Create'),
                  onPressed: () {
                    submit();
                    createItem();
                    Navigator.pop(context, true);
    
                    },
                  ),
                ],
              ),
            ),
          ),
        )
      ],
    );
      }
    
     Future scan() async {
    try {
      String barcode = await BarcodeScanner.scan();
      setState(() => this.barcode = barcode);
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.CameraAccessDenied) {
        setState(() {
          this.barcode = 'The user did not grant the camera permission!';
        });
      } else {
        setState(() => this.barcode = 'Unknown error: $e');
      }
    } on FormatException{
      setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
    } catch (e) {
      setState(() => this.barcode = 'Unknown error: $e');
        }
      }
    
    
    }
    
    Future createItem() async {
      Firestore.instance.runTransaction((Transaction transaction) async {
      CollectionReference reference = 
      Firestore.instance.collection('items');
      await reference.add({"title": newData.title, "location": newData.location, "type": newData.type,"date": _getDateNow(), "editing": false, "barcode": newData.barcode,});
    
      });
    }
    
    _getDateNow() {
      var now = new DateTime.now();
      var formatter = new DateFormat('MM-dd-yyyy H:mm');
      return formatter.format(now);
    }
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Aman Malhotra    6 年前

    对于来自字段的文本,制作一个控制器,在其中初始化字符串条形码,如下所示:

    TextEditingContoller c = new TextEditingController();
    

    在文本表单内的list tile字段中,将其控制器设置为c,并在list tile的inside ontap函数中执行此操作。

    scan().then(()=>setState((){
        c.text = barcode;
    }));
    

    然后,函数接受在编写代码时自动向您建议的参数。我只在vscode和android工作室尝试过。 因此,请确保将正确的参数传递给then函数,否则可能会出现错误。

        2
  •  0
  •   W P    6 年前

    所以,看起来then()需要声明一个类型。传入字符串修复了它。

    scan().then((String)=>setState((){
        c.text = barcode;
    }));