Skip to content

Commit

Permalink
Added AaddingSubjectScreen , Fixed divide by Zero and other attendanc…
Browse files Browse the repository at this point in the history
…e bugs, Added Reset Password Screen, Made toUpperCase to railway form fields
  • Loading branch information
SayedZeeshanHyder committed Nov 26, 2024
1 parent 4c760bd commit e29ccc4
Show file tree
Hide file tree
Showing 6 changed files with 442 additions and 44 deletions.
251 changes: 251 additions & 0 deletions lib/new_ui/screens/attendance_screen/add_attendance.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
import 'package:flutter/material.dart';
import 'package:tsec_app/new_ui/colors.dart';
import 'package:tsec_app/new_ui/screens/attendance_screen/widgets/attendanceservice.dart';

class AddAttendanceScreen extends StatefulWidget {

int? index;
final bool isUpdate;
Map<String, dynamic>? updatedSubject;
List<dynamic>? attendanceList;
AddAttendanceScreen({required this.isUpdate, this.updatedSubject,this.index,this.attendanceList});

@override
State<AddAttendanceScreen> createState() => _AddAttendanceScreenState();
}

class _AddAttendanceScreenState extends State<AddAttendanceScreen> {

late TextEditingController subjectController;
late TextEditingController totalLecturesController;
late TextEditingController attendedLecturesController;

@override
void initState() {
super.initState();
subjectController = TextEditingController(text: widget.isUpdate ? widget.updatedSubject!['subject_name']:"");
totalLecturesController = TextEditingController(text: widget.isUpdate ? widget.updatedSubject!['total'].toString():"");
attendedLecturesController = TextEditingController(text: widget.isUpdate ? widget.updatedSubject!['present'].toString():"");
}

TextStyle inputTextFieldStyle = TextStyle(color: Colors.white,);

@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
foregroundColor: Colors.white,
centerTitle: true,
actions: [
if (widget.isUpdate) IconButton(onPressed: (){
showDialog(context: context, builder: (context){
return AlertDialog(
title: Text("Are you sure you want to delete?",),
actions: [
InkWell(
onTap:(){
Navigator.pop(context);
},
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.symmetric(vertical: 5),
width: size.width*0.3,
decoration: BoxDecoration(
color: commonbgL4ightblack,
borderRadius: BorderRadius.circular(5),
),
child: Text("Cancel",style: TextStyle(color: Colors.white),),
),
),
InkWell(
onTap: () async{
await AttendanceService.deleteSubject(widget.attendanceList!, widget.index!);
Navigator.pop(context);
Navigator.pop(context);
},
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.symmetric(vertical: 5),
width: size.width*0.3,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(5),
),
child: Text("Delete",style: TextStyle(color: Colors.white),),
),
),
],
);
});
}, icon: Icon(Icons.delete,color: Colors.red,),) else SizedBox(),
],
),
body: ListView(
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Text("Add a Subject",style: TextStyle(color: Colors.white,fontSize: 23,fontWeight: FontWeight.bold),),
),

SizedBox(
height: size.height*0.04,
),

Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Text("Subject Name: ",style: TextStyle(color: Colors.white,fontSize: 17,fontWeight: FontWeight.bold),),
),

SizedBox(
height: size.height*0.01,
),
Container(
padding: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: commonbgL4ightblack,
borderRadius: BorderRadius.circular(5)
),
margin: EdgeInsets.symmetric(horizontal: 20),
child: TextField(
style: inputTextFieldStyle,
controller: subjectController,
decoration: InputDecoration(
border: InputBorder.none,
),
),
),


SizedBox(
height: size.height*0.04,
),

Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Text("Attended Lectures: ",style: TextStyle(color: Colors.white,fontSize: 17,fontWeight: FontWeight.bold),),
),

SizedBox(
height: size.height*0.01,
),
Container(
padding: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: commonbgL4ightblack,
borderRadius: BorderRadius.circular(5)
),
margin: EdgeInsets.symmetric(horizontal: 20),
child: TextField(
style: inputTextFieldStyle,
keyboardType: TextInputType.number,
controller: attendedLecturesController,
decoration: InputDecoration(
border: InputBorder.none,
),
),
),


SizedBox(
height: size.height*0.04,
),

Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Text("Total Lectures: ",style: TextStyle(color: Colors.white,fontSize: 17,fontWeight: FontWeight.bold),),
),

SizedBox(
height: size.height*0.01,
),
Container(
padding: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: commonbgL4ightblack,
borderRadius: BorderRadius.circular(5)
),
margin: EdgeInsets.symmetric(horizontal: 20),
child: TextField(
style: inputTextFieldStyle,
keyboardType: TextInputType.number,
controller: totalLecturesController,
decoration: InputDecoration(
border: InputBorder.none,
),
),
),

SizedBox(
height: size.height*0.325,
),

InkWell(
onTap: () async{
bool isSubjectAdded = await addSubject();
if(isSubjectAdded){
Navigator.pop(context);
showSnackBarMessage("Subject Added Successfully", Colors.green);
}
},
splashFactory: NoSplash.splashFactory,
child: Container(
alignment: Alignment.center,
height: 45,
width: size.width,
margin: EdgeInsets.symmetric(horizontal: 20),
decoration: BoxDecoration(
color: cardcolorblue,
borderRadius: BorderRadius.circular(5),
),
child: Text("Send Mail",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),),
),
),
],
),
);
}

Future<bool> addSubject() async{
if(totalLecturesController.text.isEmpty || attendedLecturesController.text.isEmpty || subjectController.text.isEmpty){
showSnackBarMessage("Fields cannot be left blank", Colors.red);
return false;
}
else if(totalLecturesController.text.contains(',') || totalLecturesController.text.contains('.') || attendedLecturesController.text.contains(',') || attendedLecturesController.text.contains('.')){
showSnackBarMessage("Attended and Total Lectures can only have Numeric Charachters", Colors.red);
return false;
}
String subjectName = subjectController.text;
int totalLectures =
int.parse(totalLecturesController.text);
int attendedLectures =
int.parse(attendedLecturesController.text);

if(totalLectures.isNegative || attendedLectures.isNegative){
showSnackBarMessage("Lectures cannot be Negative", Colors.red);
return false;
}else if(attendedLectures>totalLectures){
showSnackBarMessage("Attended Lectures cannot be greater than Total Lectures", Colors.red);
return false;
}
else{
Map<String, dynamic> updatedSubject = {
"subject_name": subjectName,
"total": totalLectures,
"present": attendedLectures
};
if(widget.isUpdate){
await AttendanceService.updateSubject(widget.attendanceList!, widget.index!, updatedSubject);
}else {
await AttendanceService.addSubject(updatedSubject);
}
return true;
}

}

showSnackBarMessage(String text,Color color){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(text,style: TextStyle(color: Colors.white),),backgroundColor: color,),);
}
}
60 changes: 52 additions & 8 deletions lib/new_ui/screens/attendance_screen/attendance_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:tsec_app/provider/auth_provider.dart';
import 'package:tsec_app/services/sharedprefsfordot.dart';

import '../../showcasekeys.dart';
import 'add_attendance.dart';

class AttendanceScreen extends StatefulWidget {
const AttendanceScreen({super.key});
Expand Down Expand Up @@ -183,11 +184,30 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
itemCount: attendanceList.length,
itemBuilder: (context, index) {
var attendanceInfo = attendanceList[index];
print(attendanceInfo);

return GestureDetector(
onTap: () {
// Show dialog with current values for modification
showDialog(

Navigator.push(context, PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) =>
AddAttendanceScreen(isUpdate: true,index: index,attendanceList: attendanceList,updatedSubject: attendanceInfo,),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0); // Start from bottom
const end = Offset.zero; // Move to top (center)
const curve = Curves.easeInOut;

var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
var offsetAnimation = animation.drive(tween);

return SlideTransition(
position: offsetAnimation,
child: child,
);
},
),);
/* showDialog(
context: context,
builder: (BuildContext context) {
TextEditingController subjectNameController =
Expand Down Expand Up @@ -364,7 +384,8 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
}
isEnabled = false;
/*DocumentSnapshot doc = await FirebaseFirestore.instance
*/
/*DocumentSnapshot doc = await FirebaseFirestore.instance
.collection("Attendance")
.doc(FirebaseAuth.instance.currentUser!.uid)
.get();
Expand All @@ -390,6 +411,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
.update({'attendance': attendanceList});
}
}*/
/*
},
child: Text('Update',
style: TextStyle(color: Colors.blue)),
Expand All @@ -399,7 +421,8 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
AttendanceService.deleteSubject(
attendanceList, index);
/*DocumentSnapshot doc = await FirebaseFirestore.instance
*/
/*DocumentSnapshot doc = await FirebaseFirestore.instance
.collection("Attendance")
.doc(FirebaseAuth.instance.currentUser!.uid)
.get();
Expand All @@ -417,6 +440,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
.update({'attendance': attendanceList});
}
_fetchAndSetAttendance();*/
/*
Navigator.of(context).pop();
},
child: Text('Delete',
Expand All @@ -432,7 +456,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
],
);
},
);
);*/
},
child: Card(
child: Container(
Expand Down Expand Up @@ -491,7 +515,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
height: 10,
),
LinearProgressIndicator(
value: attendanceInfo['total']
value: attendanceInfo['total'] != 0
? attendanceInfo['present'] /
attendanceInfo['total']
: 0,
Expand Down Expand Up @@ -596,7 +620,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
descTextStyle: TextStyle(fontSize: 15),
child: FloatingActionButton(
onPressed: () {
showDialog(
/*showDialog(
context: context,
builder: (BuildContext context) {
TextEditingController subjectNameController =
Expand Down Expand Up @@ -740,7 +764,8 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
"present": attendedLectures
};
AttendanceService.addSubject(updatedSubject);
/*FirebaseFirestore.instance
*/
/*FirebaseFirestore.instance
.collection("Attendance")
.doc(FirebaseAuth.instance.currentUser!.uid)
.set({
Expand All @@ -751,6 +776,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
}])
}, SetOptions(merge: true));
_fetchAndSetAttendance();*/
/*
Navigator.of(context).pop();
}
},
Expand All @@ -759,7 +785,25 @@ class _AttendanceScreenState extends State<AttendanceScreen> {
],
);
},
);
);*/

Navigator.push(context, PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) =>
AddAttendanceScreen(isUpdate: false,),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0); // Start from bottom
const end = Offset.zero; // Move to top (center)
const curve = Curves.easeInOut;

var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
var offsetAnimation = animation.drive(tween);

return SlideTransition(
position: offsetAnimation,
child: child,
);
},
),);
},
shape: CircleBorder(
side: BorderSide(
Expand Down
Loading

0 comments on commit e29ccc4

Please sign in to comment.