import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:intl/intl.dart'; import 'package:progress_dialog_null_safe/progress_dialog_null_safe.dart'; import 'dart:developer' as logDev; import '../AjukanCuti/backgroundHistory.dart'; import 'RequestHttp/historyAbsensi_post.dart'; class HistoryAbsensi extends StatefulWidget { const HistoryAbsensi({Key? key}) : super(key: key); @override State createState() => _HistoryAbsensi(); } class _HistoryAbsensi extends State { late List id_List; late List employee_name_List; late List check_in_List; late List check_out_List; late List worked_hours_List; late List dayDate_List; late List date_List; late List month_List; //late List workingHours_List; late List showCheckIn_List; late List showCheckOut_List; int HistoryLength = 0; @override initState() { super.initState(); id_List = [""]; employee_name_List = [""]; check_in_List = [""]; check_out_List = [""]; worked_hours_List = [""]; dayDate_List = [""]; date_List = [""]; month_List = [""]; //workingHours_List = [""]; showCheckIn_List = [""]; showCheckOut_List = [""]; WidgetsBinding.instance.addPostFrameCallback((_) async { getHistoryData(); }); } getHistoryData() async { ProgressDialog loading = ProgressDialog(context); loading = ProgressDialog(context, type: ProgressDialogType.normal, isDismissible: false, showLogs: true); loading.style( message: 'Please Wait .....', borderRadius: 3, backgroundColor: Colors.white, progressWidget: CircularProgressIndicator(), elevation: 10.0, padding: EdgeInsets.all(10), insetAnimCurve: Curves.easeInOut, progress: 0.0, maxProgress: 100.0, progressTextStyle: TextStyle( color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w400), messageTextStyle: TextStyle( color: Colors.black, fontSize: 15.0, fontWeight: FontWeight.w600)); loading.show(); HistoryAbsensi_Post.connectToAPI().then((valueResult) async { Map object = jsonDecode(valueResult); if (object.containsKey("result").toString() == "true") { String result = object['result'].toString(); if (result.contains("failed")) { loading.hide(); alertDialogFailedRetrievedData(context); } else { List historyAbsensi = object['result']; loading.hide(); setState(() { for (int i = 0; i < historyAbsensi.length; i++){ String id = historyAbsensi[i]['id'].toString(); String employee_name = historyAbsensi[i]['employee_name'].toString(); String check_in = historyAbsensi[i]['check_in'].toString(); String check_out = historyAbsensi[i]['check_out'].toString(); String worked_hours = historyAbsensi[i]['worked_hours'].toString(); double hours = double.parse(worked_hours); if (check_in != "false"){ //Get Day String dayDate = DateFormat('EEEE').format(DateTime.parse(check_in)); String date = DateFormat('dd').format(DateTime.parse(check_in)); String month = DateFormat('MMM').format(DateTime.parse(check_in)); //Convert UTC to Local Time - Check In Time DateTime checkInTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(check_in, true); String checkInTimeLocal = checkInTime.toLocal().toString(); String showCheckInTime = checkInTimeLocal.substring(11, 19); //logDev.log(showCheckInTime.substring(0,5), name: "SHOW CHECK IN TIME"); dayDate_List.add(dayDate.substring(0,3)); date_List.add(date); month_List.add(month); showCheckIn_List.add(showCheckInTime.substring(0,5)); } if (check_out != "false"){ //Convert UTC to Local Time - Check Out Time DateTime checkOutTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(check_out, true); String dayDateOut = DateFormat('EEEE').format(DateTime.parse(check_out)); String dateOut = DateFormat('dd').format(DateTime.parse(check_out)); String checkOutTimeLocal = checkOutTime.toLocal().toString(); String showCheckOutTime = checkOutTimeLocal.substring(11, 19); //logDev.log(showCheckOutTime, name: "SHOW CHECK OUT TIME"); showCheckOut_List.add(showCheckOutTime.substring(0,5)); } if (check_in == "false"){ String show_check_in = "-"; showCheckIn_List.add(show_check_in); } if (check_out == "false"){ String show_check_out = "-"; //String workingHours = "-"; //workingHours_List.add(workingHours); showCheckOut_List.add(show_check_out); } if (check_in != "false" && check_out != "false"){ DateFormat format = DateFormat("HH:mm"); DateTime cIn = format.parse(check_in.substring(11,16)); DateTime cOut = format.parse(check_out.substring(11,16)); //workingHours_List.add("${cOut.difference(cIn)}".substring(0,4)); } id_List.add(id); employee_name_List.add(employee_name); check_in_List.add(check_in); check_out_List.add(check_out); worked_hours_List.add(hours.toStringAsFixed(3)); } id_List.removeAt(0); employee_name_List.removeAt(0); check_in_List.removeAt(0); check_out_List.removeAt(0); worked_hours_List.removeAt(0); dayDate_List.removeAt(0); date_List.removeAt(0); month_List.removeAt(0); //workingHours_List.removeAt(0); showCheckIn_List.removeAt(0); showCheckOut_List.removeAt(0); HistoryLength = historyAbsensi.length; }); } } else { alertDialogFailedRetrievedData(context); } }); loading.hide(); } @override Widget build(BuildContext context) { var size = MediaQuery.of(context).size; return Scaffold( body: Stack( children: [ Column( children: [ Stack( children: [ WavyHeader(), Container( margin: EdgeInsets.only( top: (size.height / 6) - 20), padding: EdgeInsets.fromLTRB(0, 5, 25, 5), child: Row( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.end, children: [ Text( 'Attendance History\t\t', maxLines: 1, style: GoogleFonts.luckiestGuy( fontSize: 28, color: Color(0xFF4858A7), //fontWeight: FontWeight.bold, fontStyle: FontStyle.italic, ), ), Image.asset( 'assets/images/ic_history.png', width: 40, height: 40, ), ], ) ), ], ), ], ), Container( margin: EdgeInsets.only(top: (MediaQuery.of(context).size.height / 6) + 40, left: 5, right: 5, bottom: 10), child: ListView.builder( scrollDirection: Axis.vertical, shrinkWrap: true, itemCount: HistoryLength, itemBuilder: (context, int i) { return Container( margin: EdgeInsets.fromLTRB(5, 5, 5, 5), child: InkWell( child: Card( elevation: 8, child: Container( padding: EdgeInsets.all(10), child: Row( children: [ Expanded( flex: 2, child: Container( padding: EdgeInsets.all(5), decoration: BoxDecoration( border: Border.all(color: Colors.black), borderRadius: BorderRadius.all(Radius.circular(3)), ), child: Column( children: [ Text(dayDate_List[i], style: GoogleFonts.fredokaOne(fontSize: 16, color: Colors.blueGrey)), Text(date_List[i], style: GoogleFonts.fredokaOne(fontSize: 16, color: Colors.blueGrey)), Text(month_List[i], style: GoogleFonts.fredokaOne(fontSize: 16, color: Colors.blueGrey)), ], ), ) ), Expanded( flex: 2, child: Column( children: [ Text("Check In", style: GoogleFonts.fredokaOne(fontSize: 17),), Text(showCheckIn_List[i], style: GoogleFonts.barlowSemiCondensed(fontSize: 17),), ], ), ), Expanded( flex: 2, child: Column( children: [ Text("Check Out", style: GoogleFonts.fredokaOne(fontSize: 16),), Text(showCheckOut_List[i], style: GoogleFonts.barlowSemiCondensed(fontSize: 17),), ], ), ), Expanded( flex: 3, child: Column( children: [ Text("Working Hours", style: GoogleFonts.fredokaOne(fontSize: 16),), Text(worked_hours_List[i], style: GoogleFonts.barlowSemiCondensed(fontSize: 17),), ], ), ), ], ), ), ), onTap: (){ setState(() { //visible[i] = !visible[i]; }); }, ), ); }, ), ) ]), ); } } alertDialogFailedRetrievedData(BuildContext context) { Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => HistoryAbsensi())); }, ); Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text("Failed to Retrieve Data"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, ); } alertDialogFailedResponse(BuildContext context){ Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement(context, MaterialPageRoute( builder: (context) => HistoryAbsensi())); }, ); Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text("Server Response Error"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, ); }