step 7 : presensi selfie get address and coordinate fix
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:absensi_sas/repository/googleapis_repository.dart';
|
||||
import 'package:absensi_sas/screen/presensi/presensi_clock_in_provider.dart';
|
||||
import 'package:absensi_sas/screen/presensi/presensi_clock_out_provider.dart';
|
||||
import 'package:absensi_sas/widget/custom_drawer.dart';
|
||||
import 'package:dart_nominatim/dart_nominatim.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:geocoding/geocoding.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
// import 'package:geocoding/geocoding.dart';
|
||||
// import 'package:geolocator/geolocator.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:location/location.dart';
|
||||
import 'package:mobkit_dashed_border/mobkit_dashed_border.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
// import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../app/route.dart';
|
||||
@@ -25,6 +29,7 @@ import '../../widget/real_time.dart';
|
||||
import '../../widget/sankbar_widget.dart';
|
||||
import 'check_distance_provider.dart';
|
||||
import 'check_presensi_jam_provider.dart';
|
||||
import 'googleapis_provider.dart';
|
||||
import 'presensi_selfie_upload_area.dart';
|
||||
import 'dart:io' as io;
|
||||
|
||||
@@ -52,6 +57,9 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
final fileSize = useState(0);
|
||||
final fileName = useState("");
|
||||
|
||||
Location location = new Location();
|
||||
LocationData _locationData;
|
||||
|
||||
getBase64() async {
|
||||
// List<int> imageBytes = await fileData.value?.readAsBytes();
|
||||
if (fileData.value != null) {
|
||||
@@ -163,9 +171,10 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
try {
|
||||
isLoadingAddressUserLocation.value = true;
|
||||
// Mendapatkan posisi pengguna
|
||||
LocationPermission permission = await Geolocator.requestPermission();
|
||||
// LocationPermission permission = await Geolocator.requestPermission();
|
||||
final permission = await location.hasPermission();
|
||||
|
||||
if (permission == LocationPermission.denied) {
|
||||
if (permission == PermissionStatus.denied) {
|
||||
isLoadingAddressUserLocation.value = false;
|
||||
SanckbarWidget(context, 'Izin lokasi ditolak', snackbarType.error);
|
||||
// Handle jika pengguna menolak izin lokasi
|
||||
@@ -173,30 +182,42 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
return;
|
||||
}
|
||||
|
||||
Position position = await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high);
|
||||
// Position position = await Geolocator.getCurrentPosition(
|
||||
// locationSettings: LocationSettings(
|
||||
// accuracy: LocationAccuracy.high,
|
||||
// ),
|
||||
// );
|
||||
|
||||
location.changeSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
interval: 1000,
|
||||
distanceFilter: 5,
|
||||
);
|
||||
_locationData = await location.getLocation();
|
||||
|
||||
// Mendapatkan alamat dari posisi
|
||||
List<Placemark> placemarks = await placemarkFromCoordinates(
|
||||
position.latitude, position.longitude);
|
||||
final address = await positionToAddress(
|
||||
LatLng(_locationData.latitude!, _locationData.longitude!));
|
||||
|
||||
// final address = await positionToAddress(
|
||||
// LatLng(-7.5666203, 110.8083376),
|
||||
// );
|
||||
|
||||
// Mendapatkan alamat dari posisi
|
||||
if (positionLongitude.value.isEmpty && positionLatitude.value.isEmpty) {
|
||||
if (placemarks.isNotEmpty) {
|
||||
if (address.city != "") {
|
||||
isLoadingAddressUserLocation.value = false;
|
||||
Placemark placemark = placemarks.first;
|
||||
|
||||
ref.read(googleApisProvider.notifier).getAddressGoogleApis(
|
||||
latitude: _locationData.latitude!,
|
||||
longitude: _locationData.longitude!,
|
||||
);
|
||||
|
||||
// String address =
|
||||
// "${placemark.thoroughfare}, ${placemark.locality}, ${placemark.administrativeArea}, ${placemark.country},";
|
||||
|
||||
String address =
|
||||
"${placemark.street}, ${placemark.subLocality}, ${placemark.subAdministrativeArea}, ${placemark.postalCode}";
|
||||
print("Alamat: $address");
|
||||
|
||||
positionLatitude.value = position.latitude.toString();
|
||||
positionLongitude.value = position.longitude.toString();
|
||||
|
||||
if (address != "") {
|
||||
currentAddressUserLocation.value = address;
|
||||
}
|
||||
positionLatitude.value = _locationData.latitude.toString();
|
||||
positionLongitude.value = _locationData.longitude.toString();
|
||||
} else {
|
||||
isLoadingAddressUserLocation.value = false;
|
||||
SanckbarWidget(
|
||||
@@ -226,9 +247,10 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
}
|
||||
|
||||
Future<void> requestLocationPermission() async {
|
||||
var status = await Permission.location.request();
|
||||
// var status = await PermissionStatus.location.request();
|
||||
final status = await location.serviceEnabled();
|
||||
isLoadingAddressUserLocation.value = true;
|
||||
if (status.isGranted) {
|
||||
if (status) {
|
||||
// Izin diberikan, lanjutkan dengan mendapatkan lokasi
|
||||
getAddressFromLocation();
|
||||
} else {
|
||||
@@ -261,6 +283,19 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
}
|
||||
});
|
||||
|
||||
// googleApis address
|
||||
ref.listen(googleApisProvider, (prev, next) {
|
||||
if (next is GoogleApisStateLoading) {
|
||||
isLoadingProsesCheckDistance.value = true;
|
||||
} else if (next is GoogleApisStateError) {
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
SanckbarWidget(context, next.message, snackbarType.warning);
|
||||
} else if (next is GoogleApisStateDone) {
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
currentAddressUserLocation.value = next.model ?? "0";
|
||||
}
|
||||
});
|
||||
|
||||
// check jam presensi
|
||||
ref.listen(checkPresensiJamProvider, (prev, next) {
|
||||
if (next is CheckPresensiJamStateLoading) {
|
||||
@@ -508,7 +543,7 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
// 'Home Screen',
|
||||
'Presensi Selfie',
|
||||
'Presensi Selfie ${positionLatitude.value} , ${positionLongitude.value}',
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
color: Constant.textBlack,
|
||||
@@ -628,18 +663,18 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
onTap: () async {
|
||||
getAddressFromLocation();
|
||||
// jika sudah dapat latitude dan logitude baru panggil check distance provider
|
||||
if (positionLatitude.value.isNotEmpty &&
|
||||
positionLongitude.value.isNotEmpty) {
|
||||
// panggil check distance provider
|
||||
ref
|
||||
.read(checkDistanceProvider.notifier)
|
||||
.checkDistance(
|
||||
selectedUser?.model.staffId ?? "",
|
||||
selectedUser?.model.companyId ?? "",
|
||||
positionLatitude.value,
|
||||
positionLongitude.value,
|
||||
);
|
||||
}
|
||||
// if (positionLatitude.value.isNotEmpty &&
|
||||
// positionLongitude.value.isNotEmpty) {
|
||||
// // panggil check distance provider
|
||||
// ref
|
||||
// .read(checkDistanceProvider.notifier)
|
||||
// .checkDistance(
|
||||
// selectedUser?.model.staffId ?? "",
|
||||
// selectedUser?.model.companyId ?? "",
|
||||
// positionLatitude.value,
|
||||
// positionLongitude.value,
|
||||
// );
|
||||
// }
|
||||
},
|
||||
child: Container(
|
||||
width: Constant.getActualXPhone(
|
||||
|
||||
Reference in New Issue
Block a user