I try to place a checkboxtile which allows the user to click if they want to save their payment details for quick payment next time, but even though the user clicks it, nothing happens, the required functions do not run as expected, sorry if my code looks like a mess, im not good with flutter, i just try to make it work
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:hupcarwashcustomer/user_model/InsertSummaryReport.dart';
import 'package:hupcarwashcustomer/user_model/appointment.dart';
import 'package:hupcarwashcustomer/user_model/assignment.dart';
import 'package:hupcarwashcustomer/user_model/payment.dart';
import 'package:hupcarwashcustomer/user_model/serviceSales.dart';
import 'package:shared_preferences/shared_preferences.dart';
class paymentForm extends StatefulWidget {
final String carType;final String serviceName;
final String servicePrice;final String carName;final String hoursNeed;
final String custName; final String carPlate;final String date;
final List <String> data;final String sales;final List <String> selectedData;
final String timeStart;
final String timeEnd;
paymentForm({required this.carType, required this.serviceName,
required this.servicePrice, required this.carName, required this.hoursNeed,
required this.custName, required this.carPlate, required this.date,
required this.data, required this.sales, required this.selectedData,
required this.timeStart, required this.timeEnd});
@override
State<paymentForm> createState() => _paymentFormState();
}
class _paymentFormState extends State<paymentForm> {
TextEditingController nameController = TextEditingController();
TextEditingController cardNameController = TextEditingController();
TextEditingController cvvController = TextEditingController();
TextEditingController cardNumController = TextEditingController();
TextEditingController expController = TextEditingController();
String name = '';
void dispose() {
nameController.dispose();
cardNameController.dispose();
cvvController.dispose();
cardNumController.dispose();
expController.dispose();
super.dispose();
}
bool checkbox = false;
@override
void initState(){
loadPref();
super.initState();
}
loadPref() async{
SharedPreferences pref = await SharedPreferences.getInstance();
setState(() {
name = (pref.getString('custName')).toString();
});}
@override
Widget build(BuildContext context) {
DateTime date = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day);
String appointID = '';
int summarySales = 0;
return SafeArea(
child: Scaffold(
appBar: AppBar(title: Text('Payment Form'),),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: [
Text(name),
const SizedBox(
height: 10,
),
TextFormField(
controller: cardNameController,
decoration: const InputDecoration(border: OutlineInputBorder(), hintText: "Card Name"),
),
const SizedBox(
height: 10,
),
TextFormField(
controller: cardNumController,
decoration: const InputDecoration(border: OutlineInputBorder(), hintText: "Card Num"),
),
const SizedBox(
height: 10,
),
TextFormField(
controller: cvvController,
decoration: const InputDecoration(border: OutlineInputBorder(), hintText: "CVV"),
),
const SizedBox(
height: 10,
),
TextFormField(
controller: expController,
decoration: const InputDecoration(border: OutlineInputBorder(), hintText: "Exp Month/Year"),
),
const SizedBox(
height: 10,
),
CheckboxListTile(value: checkbox,
title: Text('Save payment details'),
onChanged: (bool? value){
setState(() {
checkbox = value!;
});
}),
const SizedBox(
height: 10,
),
ElevatedButton(onPressed: () async {
int countTimeEnd = int.parse(widget.timeEnd) 1;
String unique = '';
if(checkbox){
createPayment(payment(cardName: cardNameController.text,
cardNum: cardNumController.text, name: nameController.text,
cvv: cvvController.text, exp: expController.text),
nameController.text);
}
final userCollection = FirebaseFirestore.instance.collection("Appointment").doc();
unique = userCollection.id;
final newAppointment = Appointment(
carName: widget.carName.toString(),
forReport: '${date.month}.${date.year}',
carType: widget.carType.toString(),
serviceName: widget.serviceName.toString(),
date: widget.date.toString(),
timeStart: widget.timeStart,
timeEnd: countTimeEnd.toString(),
payment: widget.servicePrice,
custName: name,
carPlate: widget.carPlate.toString(),
status:'unassigned',
id: userCollection.id,
hoursNeed: widget.hoursNeed,
payStatus: "Paid"
).toJson();
try {
await userCollection.set(newAppointment);
} catch (e) {
print("some error occured $e");
}
createAssignment(Assignment(date: widget.date.toString(), timeStart:
widget.timeStart.toString(), carName: widget.carName.toString(),
serviceName: widget.serviceName,
timeEnd: widget.timeEnd.toString(), payment: widget.servicePrice,
custName: name,
carPlate: widget.carPlate.toString(),
employee: '', status: 'unassigned', id: unique), unique);
insertTimeSlot(widget.timeStart.toString(), widget.date.toString(),
widget.hoursNeed.toString());
createSales(serviceSales(forReport: '${date.month}.${date.year}' ,
salesType: '${widget.serviceName.toString()} Sales',
sales: widget.servicePrice.toString(), carType: widget.carType.toString(),
date: widget.date.toString()), '${widget.serviceName.toString()} Sales', unique);
DocumentSnapshot pathData = await FirebaseFirestore.instance
.collection('${date.month}.${date.year} Sales')
.doc(widget.serviceName.toString())
.get();
if (pathData.exists) {
Map<String, dynamic>? fetchDoc = pathData.data() as Map<String, dynamic>?;
summarySales = fetchDoc?['sales'];
}else if(!pathData.exists){
final collection = FirebaseFirestore.instance.collection('${date.month}.${date.year} Sales')
.doc(widget.serviceName.toString());
final newSummary = SummaryReport(
sales: 0,
serviceName: widget.serviceName.toString()
).toJson();
await collection.set(newSummary);
}
int Total = summarySales int.parse(widget.servicePrice.toString());
updateSummarySales(SummaryReport(serviceName: widget.serviceName.toString(),
sales: Total), '${date.month}.${date.year} Sales',
widget.serviceName.toString());
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Appointment has been successfully made')));
Navigator.popUntil(context, ModalRoute.withName(Navigator.defaultRouteName));
}
,child: const Text('Proceed')),
],
),
),
)
);
}
static Future createSales(serviceSales service, String serviceType, String id) async {
final userCollection = FirebaseFirestore.instance.collection(serviceType).doc(id);
final newAppointment = serviceSales(
id: id,
forReport: service.forReport,
sales: service.sales,
salesType: service.salesType,
date: service.date,
carType: service.carType
).toJson();
try {
await userCollection.set(newAppointment);
} catch (e) {
print("some error occured $e");
}
}
Future<void> insertDataTime(String timeslot, String date) async{
final docUser = FirebaseFirestore.instance.collection('AppointmentTime').
doc(date);
docUser.update({
timeslot : "appoint",
});
}
Future insertTimeSlot(String startTime, String date, String hoursNeed)async{
List <String> check = ['9','10','11','12','13','14','15','16','17'];
List <String> table = ['9-10', '10-11','11-12','12-13','13-14',
'14-15','15-16','16-17','17-18'];
int add = int.parse(hoursNeed);
int i = 0;
for(i = 0; i < check.length; i ){
if(startTime == check[i]){
for(int a = 0; a < add; a ){
insertDataTime(table[i a], date);
}
}
}
}
static Future updateSummarySales(SummaryReport summary, String serviceType,
String serviceSales) async {
final userCollection = FirebaseFirestore.instance.collection(serviceType).doc(serviceSales);
final newAppointment = SummaryReport(
sales: summary.sales,
serviceName: summary.serviceName
).toJson();
try {
await userCollection.set(newAppointment);
} catch (e) {
print("some error occured $e");
}
}
static Future createAssignment(Assignment assignment, String id) async {
final userCollection = FirebaseFirestore.instance.collection("Assignment").doc(id);
final newAssignment = Assignment(
carName: assignment.carName,
date: assignment.date,
timeStart: assignment.timeStart,
timeEnd: assignment.timeEnd,
payment: assignment.payment,
custName: assignment.custName,
carPlate: assignment.carPlate,
employee: 'none',
status: assignment.status,
serviceName: assignment.serviceName,
id: assignment.id,
).toJson();
try {
await userCollection.set(newAssignment);
} catch (e) {
print("some error occured $e");
}
}
static Future createPayment(payment pay, String id) async{
final userCollection = FirebaseFirestore.instance.collection("Payment").doc(id);
final newPayment = payment(
name: pay.name,
cardName: pay.cardName,
cardNum: pay.cardNum,
cvv: pay.cvv,
exp: pay.exp
).toJson();
try {
await userCollection.set(newPayment);
} catch (e) {
print("some error occured $e");
}
}
}
CodePudding user response:
Inside your ElevatedButton
you need to await
for your createPayment
function because it is a future
, try this:
if(checkbox){
await createPayment(payment(cardName: cardNameController.text,
cardNum: cardNumController.text, name: nameController.text,
cvv: cvvController.text, exp: expController.text),
nameController.text
);
}