Advertisement
rifki_cs29

AttendancePreviewPage

Feb 12th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:flutter_smmf_one/core/common/app_theme.dart';
  5. import 'package:flutter_smmf_one/core/widgets/custom_app_bar.dart';
  6. import 'package:flutter_smmf_one/core/widgets/custom_button.dart';
  7. import 'package:flutter_smmf_one/core/widgets/gap/gap.dart';
  8. import 'package:flutter_smmf_one/core/widgets/permission_dialog/location_permission_dialog.dart';
  9. import 'package:flutter_smmf_one/core/widgets/rotated_image.dart';
  10. import 'package:flutter_smmf_one/data/datasources/db/database_helper.dart';
  11. import 'package:flutter_smmf_one/presentation/controllers/home_controller.dart';
  12. import 'package:flutter_smmf_one/presentation/controllers/save_attendance_controller.dart';
  13. import 'package:geolocator/geolocator.dart';
  14. import 'package:get/get.dart';
  15.  
  16. final _saveAttendanceController = Get.put(SaveAttendanceController());
  17. // final _homeController = Get.put(HomeController());
  18.  
  19. class AttendacePreviewPage extends StatefulWidget {
  20. final String photoPath = Get.arguments as String;
  21.  
  22. AttendacePreviewPage({super.key});
  23.  
  24. @override
  25. State<AttendacePreviewPage> createState() => _AttendacePreviewPageState();
  26. }
  27.  
  28. class _AttendacePreviewPageState extends State<AttendacePreviewPage> {
  29. HomeController homeController = Get.find();
  30. Position? _currentPosition;
  31.  
  32. @override
  33. void initState() {
  34. _getCurrentPosition();
  35. super.initState();
  36. }
  37.  
  38. Future<bool> _handleLocationPermission() async {
  39. bool serviceEnabled;
  40. LocationPermission permission;
  41.  
  42. serviceEnabled = await Geolocator.isLocationServiceEnabled();
  43. if (!serviceEnabled) {
  44. Get.snackbar(
  45. "Error",
  46. "Location services are disabled. Please enable the services",
  47. );
  48. return false;
  49. }
  50.  
  51. permission = await Geolocator.checkPermission();
  52. if (permission == LocationPermission.denied) {
  53. permission = await Geolocator.requestPermission();
  54. if (permission == LocationPermission.denied) {
  55. Get.snackbar(
  56. "Error",
  57. "Location permissions are denied",
  58. );
  59. return false;
  60. }
  61. }
  62. if (permission == LocationPermission.deniedForever) {
  63. showDialog(
  64. context: context,
  65. builder: (context) {
  66. return const LocationPermissionDialog();
  67. },
  68. );
  69. return false;
  70. }
  71. return true;
  72. }
  73.  
  74. Future<void> _getCurrentPosition() async {
  75. final hasPermission = await _handleLocationPermission();
  76.  
  77. if (!hasPermission) return;
  78. await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
  79. .then((Position position) {
  80. setState(() => _currentPosition = position);
  81. }).catchError((e) {
  82. debugPrint(e);
  83. });
  84. }
  85.  
  86. @override
  87. Widget build(BuildContext context) {
  88. return Scaffold(
  89. appBar: CustomAppBar(
  90. titleText: 'Attendance Preview',
  91. leading: InkWell(
  92. hoverColor: colorWhite,
  93. onTap: () => Navigator.pop(context),
  94. child: Icon(
  95. CupertinoIcons.back,
  96. color: colorBlack,
  97. size: 24,
  98. ),
  99. ),
  100. ),
  101. body: Column(
  102. children: [
  103. Gap(18.h),
  104. Row(
  105. mainAxisAlignment: MainAxisAlignment.center,
  106. children: [
  107. RotatedImage(
  108. imagePath: widget.photoPath,
  109. mirrorHorizontal: true,
  110. ),
  111. ],
  112. ),
  113. Gap(16.h),
  114. Row(
  115. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  116. children: [
  117. Column(
  118. children: [
  119. Text(
  120. 'Latitude',
  121. style: blackTextStyle.copyWith(
  122. fontSize: 10.sp,
  123. fontStyle: FontStyle.italic,
  124. ),
  125. ),
  126. Gap(8.h),
  127. Text(
  128. _currentPosition?.latitude.toString() ?? '0.0',
  129. style: blackTextStyle.copyWith(
  130. fontSize: 12.sp,
  131. ),
  132. ),
  133. ],
  134. ),
  135. Column(
  136. children: [
  137. Text(
  138. 'Longitude',
  139. style: blackTextStyle.copyWith(
  140. fontSize: 10.sp,
  141. fontStyle: FontStyle.italic,
  142. ),
  143. ),
  144. Gap(8.h),
  145. Text(
  146. _currentPosition?.longitude.toString() ?? '0.0',
  147. style: blackTextStyle.copyWith(
  148. fontSize: 12.sp,
  149. ),
  150. ),
  151. ],
  152. ),
  153. ],
  154. ),
  155. Padding(
  156. padding: EdgeInsets.all(18.h),
  157. child: CustomButton.buttonRounded(
  158. onTap: _currentPosition?.latitude == null
  159. ? null
  160. : () {
  161. final loggedInUser = homeController.loggedInUser;
  162. try {
  163. _saveAttendanceController.saveAttendance(
  164. loggedInUser[DatabaseHelper.columnID],
  165. widget.photoPath,
  166. _currentPosition?.longitude.toString() ?? '0.0',
  167. _currentPosition?.latitude.toString() ?? '0.0',
  168. );
  169.  
  170. if (_saveAttendanceController.savedAttendanceId.value !=
  171. -1) {
  172. Get.offAllNamed('/profile');
  173. } else {
  174. Get.snackbar(
  175. 'Error',
  176. _saveAttendanceController
  177. .saveAttendanceMessage.value,
  178. );
  179. }
  180. } catch (e) {
  181. Get.snackbar('Error', 'Error: $e');
  182. }
  183. },
  184. color:
  185. _currentPosition?.latitude == null ? colorGrey : colorPrimary,
  186. padding: EdgeInsets.symmetric(
  187. horizontal: 16.w,
  188. vertical: 8.h,
  189. ),
  190. text: 'Absence',
  191. ),
  192. ),
  193. ],
  194. ),
  195. );
  196. }
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement