last63

Untitled

Feb 18th, 2025 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.14 KB | None | 0 0
  1. ////// LEAVE PAGE/ PERMIT
  2.  
  3. part of '../../../pages.dart';
  4.  
  5. class LeavePage extends StatefulWidget {
  6. const LeavePage({super.key});
  7.  
  8. @override
  9. State<LeavePage> createState() => _LeavePageState();
  10. }
  11.  
  12. class _LeavePageState extends State<LeavePage> {
  13. String strAddress = '';
  14. String strDate = '';
  15. String strTime = '';
  16. String strDateTime = '';
  17. int dateHour = 0;
  18. int dateMinute = 0;
  19. double dLat = 0.0;
  20. double dLong = 0.0;
  21. final nameController = TextEditingController();
  22. final fromController = TextEditingController();
  23. final untilController = TextEditingController();
  24. String dropValueCategories = "Please Choose";
  25. List<String> categoryList = <String>[
  26. 'Please Choose',
  27. "Sick",
  28. "Permission",
  29. "Other",
  30. ];
  31.  
  32. final FirebaseFirestore firestore = FirebaseFirestore.instance;
  33. String userId = FirebaseAuth.instance.currentUser?.uid ?? '';
  34.  
  35. @override
  36. void initState() {
  37. fetchUser();
  38. super.initState();
  39. }
  40.  
  41. Future<void> fetchUser() async {
  42. String userId = FirebaseAuth.instance.currentUser?.uid ?? 'Unknown';
  43.  
  44. if (userId == 'Unknown') {
  45. return;
  46. }
  47.  
  48. try {
  49. DocumentSnapshot userDoc =
  50. await firestore.collection('users').doc(userId).get();
  51.  
  52. String firstName = userDoc["first_name"];
  53. String lastName = userDoc["last_name"];
  54. String fullName = "$firstName $lastName";
  55.  
  56. setState(() {
  57. nameController.text = fullName;
  58. });
  59. } catch (e) {
  60. print("Error fetching user data: $e");
  61. }
  62. }
  63.  
  64. showAlertDialog(BuildContext context) {
  65. AlertDialog alertDialog = AlertDialog(
  66. content: Row(
  67. children: <Widget>[
  68. CircularProgressIndicator(
  69. valueColor: AlwaysStoppedAnimation<Color>(Colors.blueAccent),
  70. ),
  71. Container(
  72. margin: const EdgeInsets.only(left: 7),
  73. child: Text("Please wait..."),
  74. ),
  75. ],
  76. ),
  77. );
  78.  
  79. showDialog(
  80. barrierDismissible: false,
  81. context: context,
  82. builder: (BuildContext context) {
  83. return alertDialog;
  84. });
  85. }
  86.  
  87. @override
  88. Widget build(BuildContext context) {
  89. Size size = MediaQuery.of(context).size;
  90. return Scaffold(
  91. appBar: AppBar(
  92. title: Text("Leave Page"),
  93. ),
  94. body: SingleChildScrollView(
  95. child: Card(
  96. margin: EdgeInsets.all(10),
  97. shape: RoundedRectangleBorder(
  98. borderRadius: BorderRadius.circular(10),
  99. ),
  100. elevation: 5,
  101. color: Colors.white,
  102. child: Padding(
  103. padding: const EdgeInsets.all(8.0),
  104. child: Column(
  105. crossAxisAlignment: CrossAxisAlignment.start,
  106. children: <Widget>[
  107. Container(
  108. height: 50,
  109. decoration: BoxDecoration(
  110. color: Colors.blue,
  111. borderRadius: BorderRadius.circular(10),
  112. ),
  113. child: Row(
  114. children: <Widget>[
  115. SizedBox(
  116. width: 12,
  117. ),
  118. Icon(
  119. Icons.home_filled,
  120. color: Colors.white,
  121. ),
  122. SizedBox(
  123. width: 12,
  124. ),
  125. Text(
  126. "Please fill the form below",
  127. style: TextStyle(
  128. color: Colors.white,
  129. ),
  130. ),
  131. ],
  132. ),
  133. ),
  134. Padding(
  135. padding: const EdgeInsets.only(top: 10),
  136. child: TextField(
  137. enabled: false,
  138. textInputAction: TextInputAction.done,
  139. keyboardType: TextInputType.text,
  140. controller: nameController,
  141. decoration: InputDecoration(
  142. contentPadding:
  143. const EdgeInsets.symmetric(horizontal: 10),
  144. labelText: "Your Name",
  145. hintText: "Please enter your name",
  146. hintStyle:
  147. const TextStyle(fontSize: 14, color: Colors.grey),
  148. labelStyle:
  149. const TextStyle(fontSize: 14, color: Colors.black),
  150. border: OutlineInputBorder(
  151. borderRadius: BorderRadius.circular(10),
  152. borderSide: const BorderSide(color: Colors.blueAccent),
  153. ),
  154. enabledBorder: OutlineInputBorder(
  155. borderRadius: BorderRadius.circular(10),
  156. borderSide: const BorderSide(color: Colors.blueAccent),
  157. ),
  158. ),
  159. ),
  160. ),
  161. const Padding(
  162. padding: EdgeInsets.symmetric(vertical: 10),
  163. child: Text("Leave Type"),
  164. ),
  165. Padding(
  166. padding: EdgeInsets.all(10),
  167. child: Container(
  168. child: DropdownButton(
  169. value: dropValueCategories,
  170. items: categoryList
  171. .map((e) => DropdownMenuItem(
  172. child: Text(e),
  173. value: e,
  174. ))
  175. .toList(),
  176. onChanged: (value) {
  177. setState(() {
  178. dropValueCategories = value.toString();
  179. });
  180. },
  181. isExpanded: true,
  182. ),
  183. ),
  184. ),
  185. Padding(
  186. padding: const EdgeInsets.all(10),
  187. child: Row(
  188. children: [
  189. Expanded(
  190. child: Row(
  191. children: [
  192. Text("From"),
  193. SizedBox(
  194. width: 10,
  195. ),
  196. Expanded(
  197. child: TextField(
  198. controller: fromController,
  199. onTap: () async {
  200. DateTime? pickedDate = await showDatePicker(
  201. context: context,
  202. initialDate: DateTime.now(),
  203. firstDate: DateTime(1900),
  204. lastDate: DateTime(9999),
  205. );
  206. if (pickedDate != null) {
  207. fromController.text =
  208. DateFormat('dd/M/yyyy')
  209. .format(pickedDate);
  210. }
  211. },
  212. ),
  213. )
  214. ],
  215. ),
  216. ),
  217. Expanded(
  218. child: Row(
  219. children: [
  220. Text("Until"),
  221. SizedBox(
  222. width: 10,
  223. ),
  224. Expanded(
  225. child: TextField(
  226. controller: untilController,
  227. onTap: () async {
  228. DateTime? pickedDate = await showDatePicker(
  229. context: context,
  230. initialDate: DateTime.now(),
  231. firstDate: DateTime(1900),
  232. lastDate: DateTime(9999),
  233. );
  234. if (pickedDate != null) {
  235. untilController.text =
  236. DateFormat('dd/M/yyyy')
  237. .format(pickedDate);
  238. }
  239. },
  240. ),
  241. )
  242. ],
  243. ),
  244. ),
  245. ],
  246. ),
  247. ),
  248. Padding(
  249. padding: EdgeInsets.all(
  250. 8,
  251. ),
  252. child: Container(
  253. alignment: Alignment.center,
  254. margin: EdgeInsets.all(8),
  255. child: Material(
  256. elevation: 3,
  257. borderRadius: BorderRadius.circular(30),
  258. child: Container(
  259. width: size.width * 0.8,
  260. height: 40,
  261. decoration: BoxDecoration(
  262. borderRadius: BorderRadius.circular(30),
  263. color: Colors.amber,
  264. ),
  265. child: Material(
  266. borderRadius: BorderRadius.circular(20),
  267. color: Colors.blueAccent,
  268. child: InkWell(
  269. splashColor: Colors.blue,
  270. borderRadius: BorderRadius.circular(30),
  271. onTap: () {
  272. if (nameController.text.isEmpty ||
  273. dropValueCategories == "Please Choose" ||
  274. fromController.text.isEmpty ||
  275. untilController.text.isEmpty) {
  276. ScaffoldMessenger.of(context).showSnackBar(
  277. SnackBar(
  278. content: Row(
  279. children: [
  280. Icon(
  281. Icons.info_outline,
  282. color: Colors.white,
  283. ),
  284. SizedBox(
  285. width: 10,
  286. ),
  287. Text(
  288. "Please fill all the form",
  289. style: TextStyle(color: Colors.white),
  290. ),
  291. ],
  292. ),
  293. backgroundColor: Colors.blueAccent,
  294. shape: StadiumBorder(),
  295. behavior: SnackBarBehavior.floating,
  296. ),
  297. );
  298. } else {
  299. submitAbsent(
  300. nameController.text.toString(),
  301. dropValueCategories.toString(),
  302. fromController.text,
  303. untilController.text
  304. );
  305. }
  306. },
  307. child: Center(
  308. child: Text(
  309. "Submit",
  310. style: TextStyle(
  311. color: Colors.white,
  312. fontWeight: FontWeight.bold,
  313. ),
  314. )),
  315. ),
  316. ),
  317. ),
  318. ),
  319. ),
  320. ),
  321. ],
  322. ),
  323. ),
  324. ),
  325. ),
  326. );
  327. }
  328.  
  329. Future<void> submitAbsent(
  330. String name, String status, String from, String until) async {
  331. showLaoderDialog(context);
  332.  
  333. String userId = FirebaseAuth.instance.currentUser?.uid ?? "Unkown";
  334. print("USER ID = $userId");
  335.  
  336. if (userId == "Unkown") {
  337. print("Error : User ID not found");
  338. return;
  339. }
  340.  
  341. DocumentReference userDocRef = firestore.collection('users').doc(userId);
  342. CollectionReference attendanceCollection =
  343. userDocRef.collection('attendance');
  344.  
  345. attendanceCollection.add({
  346. 'name': name,
  347. 'description': status,
  348. 'dateTime': '$from-$until',
  349. 'createdAt': FieldValue.serverTimestamp()
  350. }).then((result) {
  351. print("Data berhasil disimpan dengan ID = ${result.id}");
  352. setState(() {
  353. Navigator.of(context).pop();
  354. ScaffoldMessenger.of(context).showSnackBar(
  355. SnackBar(
  356. content: Row(
  357. children: [
  358. Icon(
  359. Icons.check_circle_outline,
  360. color: Colors.white,
  361. ),
  362. SizedBox(
  363. width: 10,
  364. ),
  365. Text(
  366. "Yeayyy, berhasil disubmit, report successed",
  367. style: TextStyle(color: Colors.white),
  368. )
  369. ],
  370. ),
  371. backgroundColor: Colors.greenAccent,
  372. behavior: SnackBarBehavior.floating,
  373. shape: StadiumBorder(),
  374. ),
  375. );
  376. Navigator.pushReplacement(
  377. context,
  378. MaterialPageRoute(
  379. builder: (context) => const HomeAttendancePage()));
  380. });
  381. }).catchError((error) {
  382. print("Error menyimpan data $error");
  383. Navigator.of(context).pop();
  384. ScaffoldMessenger.of(context).showSnackBar(
  385. SnackBar(
  386. content: Row(
  387. children: [
  388. Icon(
  389. Icons.error_outline,
  390. color: Colors.white,
  391. ),
  392. SizedBox(
  393. width: 10,
  394. ),
  395. Text(
  396. "Ups,.. $error",
  397. style: TextStyle(color: Colors.white),
  398. )
  399. ],
  400. ),
  401. backgroundColor: Colors.blueGrey,
  402. behavior: SnackBarBehavior.floating,
  403. shape: StadiumBorder(),
  404. ),
  405. );
  406. });
  407. }
  408.  
  409. void showLaoderDialog(BuildContext context) {
  410. AlertDialog alertDialog = AlertDialog(
  411. content: Row(
  412. children: [
  413. CircularProgressIndicator(
  414. valueColor: AlwaysStoppedAnimation<Color>(Colors.blueAccent),
  415. ),
  416. Container(
  417. margin: EdgeInsets.only(left: 20),
  418. child: Text("Please wait....."),
  419. )
  420. ],
  421. ),
  422. );
  423. showDialog(
  424. barrierDismissible: false,
  425. context: context,
  426. builder: (BuildContext context) {
  427. return alertDialog;
  428. });
  429. }
  430. }
  431.  
Advertisement
Add Comment
Please, Sign In to add comment