app/lib/components/checkout_form.dart

379 lines
15 KiB
Dart
Raw Normal View History

import 'dart:convert';
2022-05-16 02:51:47 +02:00
import 'package:flutter/material.dart';
2022-04-28 04:12:46 +02:00
import 'package:flutter_form_builder/flutter_form_builder.dart';
2022-05-12 16:46:55 +02:00
import 'package:nekoya_app/api/api.dart';
import 'package:nekoya_app/data/auth.dart';
import 'package:nekoya_app/data/cart.dart';
2022-04-28 04:12:11 +02:00
2022-04-28 04:12:46 +02:00
class CheckoutForm extends StatefulWidget {
2022-04-28 21:20:36 +02:00
const CheckoutForm({Key? key}) : super(key: key);
2022-04-28 04:12:46 +02:00
@override
State<CheckoutForm> createState() => _CheckoutFormState();
}
2022-04-28 06:34:54 +02:00
final _formKey = GlobalKey<FormBuilderState>();
2022-04-28 04:12:46 +02:00
class _CheckoutFormState extends State<CheckoutForm> {
2022-05-12 17:10:04 +02:00
Future<dynamic> loadCheckout(checkoutData) async {
var session = await getSession();
var data = await checkoutPost(session, checkoutData);
return data;
2022-05-12 16:46:55 +02:00
}
Future submitForm(BuildContext context) async {
if (_formKey.currentState!.fields["First Name"]!.value.isEmpty ||
_formKey.currentState!.fields["Last Name"]!.value.isEmpty ||
_formKey.currentState!.fields["Phone Number"]!.value.isEmpty ||
_formKey.currentState!.fields["Street Address"]!.value.isEmpty ||
_formKey.currentState!.fields["Street Address 2"]!.value.isEmpty ||
_formKey.currentState!.fields["Region"]!.value.isEmpty ||
_formKey.currentState!.fields["Province"]!.value.isEmpty ||
_formKey.currentState!.fields["City"]!.value.isEmpty ||
_formKey.currentState!.fields["District"]!.value.isEmpty ||
_formKey.currentState!.fields["Subdistrict"]!.value.isEmpty ||
_formKey.currentState!.fields["Postal Code"]!.value.isEmpty ||
!(_formKey.currentState!.fields["Shipping Method"]!.value == 'JNE' ||
_formKey.currentState!.fields["Shipping Method"]!.value == 'JNT' ||
_formKey.currentState!.fields["Shipping Method"]!.value ==
'SiCepat')) {
return 999;
} else {
var currentCart = await viewCart();
var cartData = jsonEncode(currentCart).toString();
Map<String, dynamic> data = {
"firstName": _formKey.currentState!.fields["First Name"]!.value,
"lastName": _formKey.currentState!.fields["Last Name"]!.value,
"phoneNumber": _formKey.currentState!.fields["Phone Number"]!.value,
"streetAddress1":
_formKey.currentState!.fields["Street Address"]!.value,
"streetAddress2":
_formKey.currentState!.fields["Street Address 2"]!.value,
"region": _formKey.currentState!.fields["Region"]!.value,
"province": _formKey.currentState!.fields["Province"]!.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,
};
2022-05-12 17:10:04 +02:00
var statusCode = await loadCheckout(data);
return statusCode;
}
}
2022-04-28 04:12:46 +02:00
@override
Widget build(BuildContext context) {
2022-04-28 06:34:54 +02:00
return Container(
margin: const EdgeInsets.fromLTRB(10, 10, 10, 5),
child: Card(
color: const Color(0xff212226),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
child: Container(
margin: const EdgeInsets.fromLTRB(5, 20, 5, 20),
padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25),
child: FormBuilder(
key: _formKey,
child: Column(
children: [
const Center(
child: Text(
"Shipping Details",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 30),
),
),
const SizedBox(
height: 20,
),
const Row(
children: [
2022-04-28 06:34:54 +02:00
Text(
"Contact Information",
style: TextStyle(
color: Colors.white70,
fontWeight: FontWeight.bold,
fontSize: 20),
),
],
),
const SizedBox(
height: 10,
),
2022-05-09 03:55:46 +02:00
makeInput(
label: "First Name",
icon: Icons.person,
keyboardType: TextInputType.name),
makeInput(
label: "Last Name",
icon: Icons.person,
keyboardType: TextInputType.name),
makeInput(
label: "Phone Number",
icon: Icons.phone_rounded,
keyboardType: TextInputType.phone),
2022-04-28 06:34:54 +02:00
const SizedBox(
height: 20,
),
const Row(
children: [
2022-04-28 06:34:54 +02:00
Text(
"Address",
style: TextStyle(
color: Colors.white70,
fontWeight: FontWeight.bold,
fontSize: 20),
),
],
),
const SizedBox(
height: 10,
),
2022-05-09 03:55:46 +02:00
makeInput(
label: "Street Address",
icon: Icons.home,
keyboardType: TextInputType.streetAddress),
makeInput(
label: "Street Address 2",
icon: Icons.home,
keyboardType: TextInputType.streetAddress),
makeInput(
label: "Region",
icon: Icons.location_city,
keyboardType: TextInputType.streetAddress),
makeInput(
label: "Province",
icon: Icons.location_city,
keyboardType: TextInputType.streetAddress),
2022-05-09 03:55:46 +02:00
makeInput(
label: "City",
icon: Icons.location_city,
keyboardType: TextInputType.streetAddress),
2022-05-09 03:55:46 +02:00
makeInput(
label: "District",
icon: Icons.location_city,
keyboardType: TextInputType.streetAddress),
2022-05-09 03:55:46 +02:00
makeInput(
label: "Subdistrict",
icon: Icons.location_city,
keyboardType: TextInputType.streetAddress),
2022-05-09 03:55:46 +02:00
makeInput(
label: "Postal Code",
icon: Icons.location_city,
keyboardType: TextInputType.number),
2022-04-28 06:34:54 +02:00
const SizedBox(
height: 20,
),
const Row(
children: [
2022-04-28 06:34:54 +02:00
Text(
"Shipping Methods",
style: TextStyle(
color: Colors.white70,
fontWeight: FontWeight.bold,
fontSize: 20),
),
],
),
const SizedBox(
height: 10,
),
Container(
decoration: BoxDecoration(
color: const Color(0xff1b1c1e),
borderRadius: BorderRadius.circular(10.0),
boxShadow: const [
BoxShadow(
color: Colors.black12,
blurRadius: 6.0,
offset: Offset(0, 2),
2022-04-28 06:34:54 +02:00
),
],
),
child: FormBuilderDropdown(
name: "Shipping Method",
dropdownColor: const Color(0xff212226),
style: const TextStyle(color: Colors.white),
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(
vertical: 0, horizontal: 10),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.red,
),
2022-04-28 06:34:54 +02:00
),
enabledBorder: InputBorder.none,
border: InputBorder.none,
2022-04-28 06:34:54 +02:00
),
items: const [
DropdownMenuItem(
value: "JNE",
child: Text("JNE"),
),
DropdownMenuItem(
value: "JNT",
child: Text("JNT"),
),
DropdownMenuItem(
value: "SiCepat",
child: Text("SiCepat"),
),
],
2022-04-28 06:34:54 +02:00
),
),
const SizedBox(
height: 20,
2022-04-28 06:34:54 +02:00
),
],
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 35),
child: Container(
padding: const EdgeInsets.only(top: 3, left: 3),
child: MaterialButton(
2022-05-23 04:03:36 +02:00
padding: const EdgeInsets.all(15.0),
2022-04-28 06:34:54 +02:00
minWidth: double.infinity,
height: 35,
onPressed: () {
2022-05-12 16:46:55 +02:00
checkSessionExist().then((isLoggedIn) {
if (isLoggedIn) {
submitForm(context).then((statusCode) {
if (statusCode == 201) {
Navigator.pushReplacementNamed(
context, '/payment');
2022-05-12 16:46:55 +02:00
} else if (statusCode == 999) {
showAlertDialog(context);
}
});
} else {
Navigator.pushNamed(context, '/login');
}
});
},
2022-04-28 06:34:54 +02:00
color: const Color(0xff8B0000),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40)),
child: const Text(
"Pay",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
2022-05-23 04:03:36 +02:00
fontSize: 20,
2022-04-28 06:34:54 +02:00
),
),
),
),
),
],
),
],
),
),
),
);
2022-04-28 04:12:46 +02:00
}
}
2022-04-28 06:34:54 +02:00
Widget makeInput({label, obscureText = false, icon, keyboardType}) {
2022-04-28 06:34:54 +02:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: const TextStyle(
fontSize: 12, fontWeight: FontWeight.bold, color: Colors.white),
),
const SizedBox(
height: 10,
2022-04-28 06:34:54 +02:00
),
2022-05-09 03:51:26 +02:00
Container(
decoration: BoxDecoration(
color: const Color(0xff1b1c1e),
borderRadius: BorderRadius.circular(10.0),
boxShadow: const [
BoxShadow(
color: Colors.black12,
blurRadius: 6.0,
offset: Offset(0, 2),
2022-04-28 06:34:54 +02:00
),
2022-05-09 03:51:26 +02:00
],
),
child: FormBuilderTextField(
initialValue: "",
name: label,
keyboardType: keyboardType,
2022-05-09 03:51:26 +02:00
obscureText: obscureText,
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
prefixIcon: Icon(
icon,
2022-04-28 06:34:54 +02:00
color: Colors.white,
),
2022-05-09 03:51:26 +02:00
contentPadding: const EdgeInsets.only(top: 14.0),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.red,
),
borderRadius: BorderRadius.circular(10.0)),
enabledBorder: InputBorder.none,
border: InputBorder.none,
2022-04-28 06:34:54 +02:00
),
),
),
const SizedBox(
height: 10,
)
],
);
}
showAlertDialog(BuildContext context) {
Widget okButton = TextButton(
child: const Text("OK", style: TextStyle(color: Colors.red)),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
);
AlertDialog alert = AlertDialog(
backgroundColor: const Color(0xff1b1c1e),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
title: const Text(
"Error",
style: TextStyle(color: Colors.white),
),
content: const Text(
"Make sure to fill all fields",
style: TextStyle(color: Colors.white70),
),
actions: [
okButton,
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}