Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:flutter_smmf_one/core/common/app_theme.dart';
- import 'package:flutter_smmf_one/core/widgets/custom_app_bar.dart';
- import 'package:flutter_smmf_one/core/widgets/custom_button.dart';
- import 'package:flutter_smmf_one/core/widgets/gap/gap.dart';
- import 'package:flutter_smmf_one/core/widgets/permission_dialog/location_permission_dialog.dart';
- import 'package:flutter_smmf_one/core/widgets/rotated_image.dart';
- import 'package:flutter_smmf_one/data/datasources/db/database_helper.dart';
- import 'package:flutter_smmf_one/presentation/controllers/home_controller.dart';
- import 'package:flutter_smmf_one/presentation/controllers/save_attendance_controller.dart';
- import 'package:geolocator/geolocator.dart';
- import 'package:get/get.dart';
- final _saveAttendanceController = Get.put(SaveAttendanceController());
- // final _homeController = Get.put(HomeController());
- class AttendacePreviewPage extends StatefulWidget {
- final String photoPath = Get.arguments as String;
- AttendacePreviewPage({super.key});
- @override
- State<AttendacePreviewPage> createState() => _AttendacePreviewPageState();
- }
- class _AttendacePreviewPageState extends State<AttendacePreviewPage> {
- HomeController homeController = Get.find();
- Position? _currentPosition;
- @override
- void initState() {
- _getCurrentPosition();
- super.initState();
- }
- Future<bool> _handleLocationPermission() async {
- bool serviceEnabled;
- LocationPermission permission;
- serviceEnabled = await Geolocator.isLocationServiceEnabled();
- if (!serviceEnabled) {
- Get.snackbar(
- "Error",
- "Location services are disabled. Please enable the services",
- );
- return false;
- }
- permission = await Geolocator.checkPermission();
- if (permission == LocationPermission.denied) {
- permission = await Geolocator.requestPermission();
- if (permission == LocationPermission.denied) {
- Get.snackbar(
- "Error",
- "Location permissions are denied",
- );
- return false;
- }
- }
- if (permission == LocationPermission.deniedForever) {
- showDialog(
- context: context,
- builder: (context) {
- return const LocationPermissionDialog();
- },
- );
- return false;
- }
- return true;
- }
- Future<void> _getCurrentPosition() async {
- final hasPermission = await _handleLocationPermission();
- if (!hasPermission) return;
- await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
- .then((Position position) {
- setState(() => _currentPosition = position);
- }).catchError((e) {
- debugPrint(e);
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: CustomAppBar(
- titleText: 'Attendance Preview',
- leading: InkWell(
- hoverColor: colorWhite,
- onTap: () => Navigator.pop(context),
- child: Icon(
- CupertinoIcons.back,
- color: colorBlack,
- size: 24,
- ),
- ),
- ),
- body: Column(
- children: [
- Gap(18.h),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- RotatedImage(
- imagePath: widget.photoPath,
- mirrorHorizontal: true,
- ),
- ],
- ),
- Gap(16.h),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: [
- Column(
- children: [
- Text(
- 'Latitude',
- style: blackTextStyle.copyWith(
- fontSize: 10.sp,
- fontStyle: FontStyle.italic,
- ),
- ),
- Gap(8.h),
- Text(
- _currentPosition?.latitude.toString() ?? '0.0',
- style: blackTextStyle.copyWith(
- fontSize: 12.sp,
- ),
- ),
- ],
- ),
- Column(
- children: [
- Text(
- 'Longitude',
- style: blackTextStyle.copyWith(
- fontSize: 10.sp,
- fontStyle: FontStyle.italic,
- ),
- ),
- Gap(8.h),
- Text(
- _currentPosition?.longitude.toString() ?? '0.0',
- style: blackTextStyle.copyWith(
- fontSize: 12.sp,
- ),
- ),
- ],
- ),
- ],
- ),
- Padding(
- padding: EdgeInsets.all(18.h),
- child: CustomButton.buttonRounded(
- onTap: _currentPosition?.latitude == null
- ? null
- : () {
- final loggedInUser = homeController.loggedInUser;
- try {
- _saveAttendanceController.saveAttendance(
- loggedInUser[DatabaseHelper.columnID],
- widget.photoPath,
- _currentPosition?.longitude.toString() ?? '0.0',
- _currentPosition?.latitude.toString() ?? '0.0',
- );
- if (_saveAttendanceController.savedAttendanceId.value !=
- -1) {
- Get.offAllNamed('/profile');
- } else {
- Get.snackbar(
- 'Error',
- _saveAttendanceController
- .saveAttendanceMessage.value,
- );
- }
- } catch (e) {
- Get.snackbar('Error', 'Error: $e');
- }
- },
- color:
- _currentPosition?.latitude == null ? colorGrey : colorPrimary,
- padding: EdgeInsets.symmetric(
- horizontal: 16.w,
- vertical: 8.h,
- ),
- text: 'Absence',
- ),
- ),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement