扫描后如何将条形码值传递到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);
}