checkout_form - Fixed error handling on forms

This commit is contained in:
Matthew Patrick 2022-05-12 11:03:12 +07:00
parent 34f43c3124
commit 5994985801

View file

@ -16,79 +16,41 @@ final _formKey = GlobalKey<FormBuilderState>();
class _CheckoutFormState extends State<CheckoutForm> { class _CheckoutFormState extends State<CheckoutForm> {
Future submitForm(BuildContext context) async { Future submitForm(BuildContext context) async {
if (_formKey.currentState!.fields["First Name"]! if (_formKey.currentState!.fields["First Name"]!.value.isEmpty ||
.value == _formKey.currentState!.fields["Last Name"]!.value.isEmpty ||
'' || _formKey.currentState!.fields["Phone Number"]!.value.isEmpty ||
_formKey.currentState!.fields["Last Name"]! _formKey.currentState!.fields["Street Address"]!.value.isEmpty ||
.value == _formKey.currentState!.fields["Street Address 2"]!.value.isEmpty ||
'' || _formKey.currentState!.fields["Region"]!.value.isEmpty ||
_formKey.currentState!.fields["Phone Number"]! _formKey.currentState!.fields["Province"]!.value.isEmpty ||
.value == _formKey.currentState!.fields["City"]!.value.isEmpty ||
'' || _formKey.currentState!.fields["District"]!.value.isEmpty ||
_formKey.currentState!.fields["Street Address"]! _formKey.currentState!.fields["Subdistrict"]!.value.isEmpty ||
.value == _formKey.currentState!.fields["Postal Code"]!.value.isEmpty ||
'' || !(_formKey.currentState!.fields["Shipping Method"]!.value == 'JNE' ||
_formKey.currentState!.fields["Street Address 2"]! _formKey.currentState!.fields["Shipping Method"]!.value == 'JNT' ||
.value == _formKey.currentState!.fields["Shipping Method"]!.value ==
'' ||
_formKey
.currentState!.fields["Region"]!.value ==
'' ||
_formKey.currentState!.fields["Province"]!
.value ==
'' ||
_formKey
.currentState!.fields["City"]!.value ==
'' ||
_formKey.currentState!.fields["District"]!
.value ==
'' ||
_formKey.currentState!.fields["Subdistrict"]!
.value ==
'' ||
_formKey.currentState!.fields["Postal Code"]!
.value ==
'' ||
!(_formKey.currentState!
.fields["Shipping Method"]!.value ==
'JNE' ||
_formKey.currentState!
.fields["Shipping Method"]!.value ==
'JNT' ||
_formKey.currentState!
.fields["Shipping Method"]!.value ==
'SiCepat')) { 'SiCepat')) {
showAlertDialog(context); return 999;
return 400;
} else { } else {
var currentCart = await viewCart(); var currentCart = await viewCart();
var cartData = jsonEncode(currentCart).toString(); var cartData = jsonEncode(currentCart).toString();
Map<String, dynamic> data = { Map<String, dynamic> data = {
"firstName": _formKey "firstName": _formKey.currentState!.fields["First Name"]!.value,
.currentState!.fields["First Name"]!.value, "lastName": _formKey.currentState!.fields["Last Name"]!.value,
"lastName": _formKey "phoneNumber": _formKey.currentState!.fields["Phone Number"]!.value,
.currentState!.fields["Last Name"]!.value, "streetAddress1":
"phoneNumber": _formKey _formKey.currentState!.fields["Street Address"]!.value,
.currentState!.fields["Phone Number"]!.value, "streetAddress2":
"streetAddress1": _formKey.currentState! _formKey.currentState!.fields["Street Address 2"]!.value,
.fields["Street Address"]!.value, "region": _formKey.currentState!.fields["Region"]!.value,
"streetAddress2": _formKey.currentState! "province": _formKey.currentState!.fields["Province"]!.value,
.fields["Street Address 2"]!.value, "city": _formKey.currentState!.fields["City"]!.value,
"region": _formKey "district": _formKey.currentState!.fields["District"]!.value,
.currentState!.fields["Region"]!.value, "subDistrict": _formKey.currentState!.fields["Subdistrict"]!.value,
"province": _formKey "postalCode": _formKey.currentState!.fields["Postal Code"]!.value,
.currentState!.fields["Province"]!.value, "logistic": _formKey.currentState!.fields["Shipping Method"]!.value,
"city":
_formKey.currentState!.fields["City"]!.value,
"district": _formKey
.currentState!.fields["District"]!.value,
"subDistrict": _formKey
.currentState!.fields["Subdistrict"]!.value,
"postalCode": _formKey
.currentState!.fields["Postal Code"]!.value,
"logistic": _formKey.currentState!
.fields["Shipping Method"]!.value,
"data": cartData, "data": cartData,
}; };
@ -262,6 +224,8 @@ class _CheckoutFormState extends State<CheckoutForm> {
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => const Payment())); builder: (context) => const Payment()));
} else if (statusCode == 999) {
showAlertDialog(context);
} }
}); });
}, },