我有一个有状态的小部件,其中显示了一个用于存储信息的表格。该表单还包含一个图像容器,该图像容器存储从相机捕获的用户图像。最初,我显示一个占位符图像。但是当在捕获图像后返回到表单时,图像小部件中的图像没有得到更新。
我已经在onPress方法中使用了set state方法。
setState(() {
formpath = bgPath;
});
新的图像路径应在图像小部件中呈现
class FormScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<FormScreen> {
GlobalKey<FormState> _key = new GlobalKey();
String strText = 'Take Photo';
final TextEditingController _nameEditingController =
new TextEditingController();
final TextEditingController _ageEditingController =
new TextEditingController();
final TextEditingController _reasonEditingController =
new TextEditingController();
final TextEditingController _addressEditingController =
new TextEditingController();
final TextEditingController _idEditingController =
new TextEditingController();
final TextEditingController _personVisitingEditingController =
new TextEditingController();
final TextEditingController _doorNumberController =
new TextEditingController();
final TextEditingController _numberEditingController =
new TextEditingController();
File picture = null;
String path = "";
final List<VisitorItem> _itemList = <VisitorItem>[];
var db = new DatabaseHelper();
void _handleSubmit(
String name,
String age,
String reason,
String address,
String id,
String personVisiting,
String doorNumber,
String number,
String path) async {
VisitorItem visitorItem = new VisitorItem(
name,
age,
reason,
address,
id,
personVisiting,
doorNumber,
number,
new DateFormat.yMd().add_jm().format(DateTime.now()),
"Enter Out Time",
path);
int savedItemId = await db.saveItem(visitorItem);
VisitorItem addedItem = await db.getItem(savedItemId);
setState(() {
_itemList.insert(0, addedItem);
});
debugPrint(path);
print("Item saved id: $savedItemId");
}
@override
Widget build(BuildContext context) {
String formpath = '';
//'assets/placeholder.png';
return Scaffold(
appBar: AppBar(
title: Text('Visitor Information Entry Form'),
backgroundColor: Colors.redAccent,
),
body: Center(
child: SingleChildScrollView(
child: new Form(
key: _key,
child: new Column(
children: <Widget>[
new TextFormField(
controller: _nameEditingController,
autofocus: false,
validator: (String value) {
if ((value.isEmpty)) {
return "Name is Required";
} else if (!RegExp(r"^[a-zA-Z0-
9]+$").hasMatch(value)) {
return "Special Charaters are not allowed";
}
},
decoration: new InputDecoration(
labelText: "Name of the visitor",
hintText: "eg. Rahul",
icon: new Icon(Icons.note_add)),
),
new TextFormField(
controller: _ageEditingController,
keyboardType: TextInputType.number,
autofocus: false,
validator: (String value) {
if (value.isEmpty) {
return "Age is Required";
}
},
decoration: new InputDecoration(
labelText: "Age",
hintText: "eg. 25 yrs",
icon: new Icon(Icons.cake)),
),
new TextFormField(
controller: _reasonEditingController,
autofocus: false,
validator: (String value) {
if (value.isEmpty) {
return "Reason is Required";
}
},
decoration: new InputDecoration(
labelText: "Reason for Visit",
hintText: "For interview or AC repair",
icon: new Icon(Icons.assignment_late)),
),
new TextFormField(
controller: _addressEditingController,
autofocus: false,
validator: (String value) {
if (value.isEmpty) {
return "Address is Required";
}
},
decoration: new InputDecoration(
labelText: "Address",
hintText: "flat 110, sunrise apartment",
icon: new Icon(Icons.add_location)),
),
new TextFormField(
controller: _idEditingController,
autofocus: false,
validator: (String value) {
if (value.isEmpty) {
return "ID is Required";
}
},
decoration: new InputDecoration(
labelText: "ID",
hintText: "Your Id Number eg: DL number",
icon: new Icon(Icons.perm_identity)),
),
new TextFormField(
controller: _personVisitingEditingController,
autofocus: false,
validator: (String value) {
if (value.isEmpty) {
return "Visitor name is Required";
}
},
decoration: new InputDecoration(
labelText: "Resident Name",
hintText: "Ramesh",
icon: new Icon(Icons.visibility)),
),
new TextFormField(
controller: _doorNumberController,
autofocus: false,
validator: (String value) {
if (value.isEmpty) {
return "Door name is Required";
}
},
decoration: new InputDecoration(
labelText: "Door Number",
hintText: "10",
icon: new Icon(Icons.visibility)),
),
new TextFormField(
controller: _numberEditingController,
autofocus: false,
keyboardType: TextInputType.number,
maxLength: 10,
validator: (String value) {
if (value.isEmpty) {
return "Phone is Required";
}
},
decoration: new InputDecoration(
labelText: "Phone Number",
hintText: "+91 912345678",
icon: new Icon(Icons.phone)),
),
new Container(
child: new Column(
children: <Widget>[
new Container(
height: 120.0,
width: 120.0,
decoration: new BoxDecoration(
image: DecorationImage(
image: new AssetImage(formpath),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
)),
RaisedButton(
padding: const EdgeInsets.all(8.0),
textColor: Colors.white,
color: Colors.blue,
elevation: 4.0,
splashColor: Colors.amber,
child: new Text(strText),
onPressed: () async {
picture = await ImagePicker.pickImage(
source: ImageSource.camera,
);
final key = _numberEditingController.text;
Directory appDocDir =
await getApplicationDocumentsDirectory();
String bgPath =
appDocDir.uri.resolve(key).path;
File bgFile = await picture.copy(bgPath);
debugPrint(bgPath);
path = bgPath;
formpath = bgPath;
setState(() {
formpath = bgPath;
});
},
)
],
),
),
new FlatButton(
onPressed: () {
if (_key.currentState.validate()) {
_handleSubmit(
_nameEditingController.text,
_ageEditingController.text,
_reasonEditingController.text,
_addressEditingController.text,
_idEditingController.text,
_personVisitingEditingController.text,
_doorNumberController.text,
_numberEditingController.text,
path);
_nameEditingController.clear();
_ageEditingController.clear();
_reasonEditingController.clear();
_addressEditingController.clear();
_idEditingController.clear();
_personVisitingEditingController.clear();
_doorNumberController.clear();
_numberEditingController.clear();
path = "";
Navigator.pop(context);
}
},
child: Text("Save")),
new FlatButton(
onPressed: () {
_nameEditingController.clear();
_ageEditingController.clear();
_reasonEditingController.clear();
_addressEditingController.clear();
_idEditingController.clear();
_personVisitingEditingController.clear();
_doorNumberController.clear();
_numberEditingController.clear();
path = "";
Navigator.pop(context);
},
child: Text("Cancel"))
],
),
),
),
),
);
}
}