是否可以在Flutter中保存TextFormField中hintText的值?
Is it possible to save the value of hintText in TextFormField in Flutter?
我需要将 hintText 中的值保存到 firebase。这是从上一页获取的值,我需要将其存储到数据库中。我可以存储用户输入的其他值,但不能存储提示文本。有什么办法可以挽救吗?
这是我的小部件代码:
//This is what I cannot save it
Widget getFacilityName() => TextFormField(
//enableInteractiveSelection: false,
readOnly: true,
decoration: InputDecoration(
labelText: 'Hospital / Clinic Name',
floatingLabelBehavior: FloatingLabelBehavior.always,
hintText: widget.facilityName,
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.local_hospital),
),
// validator: (value) {
// if (value.length < 1) {
// return 'Please enter your name';
// } else {
// return null;
// }
// },
onSaved: (hintText) => setState(() => facName = hintText),
);
Widget buildName() => TextFormField(
decoration: InputDecoration(
labelText: 'Name',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.person),
),
validator: (value) {
if (value.length < 1) {
return 'Please enter your name';
} else {
return null;
}
},
onSaved: (value) => setState(() => name = value),
);
Widget buildIc() => TextFormField(
inputFormatters: [maskFormatter1],
decoration: InputDecoration(
labelText: 'IC Number',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.credit_card),
),
validator: (value) {
if (value.length < 1) {
return 'Please enter your IC number';
} else {
return null;
}
},
onSaved: (value) => setState(() => ic = value),
);
Widget buildPhoneNo() => TextFormField(
inputFormatters: [maskFormatter2],
decoration: InputDecoration(
labelText: 'Phone Number',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.local_phone),
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your phone number';
}
else if (value.length < 13 || value.length > 14) {
return 'Please enter a valid phone number';
} else {
return null;
}
},
onSaved: (value) => setState(() => phoneNo = value),
);
Widget buildDate() => DateTimeFormField(
decoration: InputDecoration(
labelText: 'Select Date & Time',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.event_note),
),
firstDate: DateTime(DateTime.now().year),
lastDate: DateTime(DateTime.now().year + 5),
validator: (value) {
if (value == null) {
return 'Please select a date';
}
else if(!value.isAfter(DateTime.now())){
return 'Cannot book the date in the past';
} else {
return null;
}
},
onSaved: (value) => setState(() => date = value),
);
Widget buildSubmit() => TextButton(
child: Text('Confirm Appointment',
style: TextStyle(
fontSize: 18.0,
),),
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(28.0),
)
),
backgroundColor: MaterialStateProperty.all<Color>(Colors.blueGrey[300]),
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
),
onPressed: () {
final isValid = formKey.currentState.validate(); //check the validator
//to hide keyboard after click submit
FocusScope.of(context).unfocus();
if(isValid) {
formKey.currentState.save();
FirebaseFirestore.instance.runTransaction((Transaction transaction) async{
CollectionReference reference = FirebaseFirestore.instance.collection('Appointment');
await reference.add({"facilityName": "$facName", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
});
}
},
);
在我的 firestore 中,我是这样的:
Screenshot from FireStore
facilityName 为空。
如果您从上一页获得 hintText
(widget.facilityName
的值),您应该直接在您的 buildSubmit
中使用它(因为它是只读的,用户不会更改它!)如下:
FirebaseFirestore.instance.runTransaction((Transaction transaction) async{
CollectionReference reference = FirebaseFirestore.instance.collection('Appointment');
await reference.add({"facilityName": "${widget.facilityName}", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
});
你想要做的不是保存 hintText
的值,实际上你想将来自上一页的变量 facilityName
的值保存到 firebase 中。
所以只需将 facilityName
的值发送到 firebase。
只需将 widget.facilityName
的值添加到 facName
这一行之前
await reference.add({"facilityName": "$facName", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
代码将是这样的:
//***
if(isValid) {
formKey.currentState.save();
FirebaseFirestore.instance.runTransaction((Transaction transaction) async{
CollectionReference reference = FirebaseFirestore.instance.collection('Appointment');
String facName = widget.facilityName;
await reference.add({"facilityName": "$facName", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
});
}
//***
并删除不需要的代码
我需要将 hintText 中的值保存到 firebase。这是从上一页获取的值,我需要将其存储到数据库中。我可以存储用户输入的其他值,但不能存储提示文本。有什么办法可以挽救吗?
这是我的小部件代码:
//This is what I cannot save it
Widget getFacilityName() => TextFormField(
//enableInteractiveSelection: false,
readOnly: true,
decoration: InputDecoration(
labelText: 'Hospital / Clinic Name',
floatingLabelBehavior: FloatingLabelBehavior.always,
hintText: widget.facilityName,
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.local_hospital),
),
// validator: (value) {
// if (value.length < 1) {
// return 'Please enter your name';
// } else {
// return null;
// }
// },
onSaved: (hintText) => setState(() => facName = hintText),
);
Widget buildName() => TextFormField(
decoration: InputDecoration(
labelText: 'Name',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.person),
),
validator: (value) {
if (value.length < 1) {
return 'Please enter your name';
} else {
return null;
}
},
onSaved: (value) => setState(() => name = value),
);
Widget buildIc() => TextFormField(
inputFormatters: [maskFormatter1],
decoration: InputDecoration(
labelText: 'IC Number',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.credit_card),
),
validator: (value) {
if (value.length < 1) {
return 'Please enter your IC number';
} else {
return null;
}
},
onSaved: (value) => setState(() => ic = value),
);
Widget buildPhoneNo() => TextFormField(
inputFormatters: [maskFormatter2],
decoration: InputDecoration(
labelText: 'Phone Number',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.local_phone),
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your phone number';
}
else if (value.length < 13 || value.length > 14) {
return 'Please enter a valid phone number';
} else {
return null;
}
},
onSaved: (value) => setState(() => phoneNo = value),
);
Widget buildDate() => DateTimeFormField(
decoration: InputDecoration(
labelText: 'Select Date & Time',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.event_note),
),
firstDate: DateTime(DateTime.now().year),
lastDate: DateTime(DateTime.now().year + 5),
validator: (value) {
if (value == null) {
return 'Please select a date';
}
else if(!value.isAfter(DateTime.now())){
return 'Cannot book the date in the past';
} else {
return null;
}
},
onSaved: (value) => setState(() => date = value),
);
Widget buildSubmit() => TextButton(
child: Text('Confirm Appointment',
style: TextStyle(
fontSize: 18.0,
),),
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(28.0),
)
),
backgroundColor: MaterialStateProperty.all<Color>(Colors.blueGrey[300]),
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
),
onPressed: () {
final isValid = formKey.currentState.validate(); //check the validator
//to hide keyboard after click submit
FocusScope.of(context).unfocus();
if(isValid) {
formKey.currentState.save();
FirebaseFirestore.instance.runTransaction((Transaction transaction) async{
CollectionReference reference = FirebaseFirestore.instance.collection('Appointment');
await reference.add({"facilityName": "$facName", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
});
}
},
);
在我的 firestore 中,我是这样的: Screenshot from FireStore
facilityName 为空。
如果您从上一页获得 hintText
(widget.facilityName
的值),您应该直接在您的 buildSubmit
中使用它(因为它是只读的,用户不会更改它!)如下:
FirebaseFirestore.instance.runTransaction((Transaction transaction) async{
CollectionReference reference = FirebaseFirestore.instance.collection('Appointment');
await reference.add({"facilityName": "${widget.facilityName}", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
});
你想要做的不是保存 hintText
的值,实际上你想将来自上一页的变量 facilityName
的值保存到 firebase 中。
所以只需将 facilityName
的值发送到 firebase。
只需将 widget.facilityName
的值添加到 facName
这一行之前
await reference.add({"facilityName": "$facName", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
代码将是这样的:
//***
if(isValid) {
formKey.currentState.save();
FirebaseFirestore.instance.runTransaction((Transaction transaction) async{
CollectionReference reference = FirebaseFirestore.instance.collection('Appointment');
String facName = widget.facilityName;
await reference.add({"facilityName": "$facName", "name": "$name", "ic": "$ic", "phoneno": "$phoneNo", "datetime": "$date"});
});
}
//***
并删除不需要的代码