step 10 : check distance selfie or not from back end
This commit is contained in:
@@ -1,16 +1,134 @@
|
||||
import 'package:absensi_sas_flutter/app/constant.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:google_sign_in/google_sign_in.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
import '../../app/route.dart';
|
||||
import '../../provider/current_check_distance_provider.dart';
|
||||
import '../../provider/current_user_provider.dart';
|
||||
import '../../provider/google_login_provider.dart';
|
||||
import '../../widget/real_date.dart';
|
||||
import '../../widget/real_time.dart';
|
||||
import '../../widget/sankbar_widget.dart';
|
||||
import '../presensi/check_distance_provider.dart';
|
||||
|
||||
class HomeScreen extends HookConsumerWidget {
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
|
||||
final selectedUser = ref.read(currentUserProvider);
|
||||
final isLoadingProsesCheckDistance = useState<bool>(false);
|
||||
final varCurrentDistanceProvider = ref.watch(currentCheckDistanceProvider);
|
||||
final positionLatitude = useState<String>("");
|
||||
final positionLongitude = useState<String>("");
|
||||
GoogleSignInAccount? currentUserGoogle =
|
||||
ref.watch(currentUserGoogleProvider);
|
||||
|
||||
useEffect(() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
||||
final staffID = ref.read(currentUserProvider)?.model.staffId ?? "0";
|
||||
if (staffID == "0") {
|
||||
//not login
|
||||
Navigator.of(context)
|
||||
.pushNamedAndRemoveUntil(loginRoute, (route) => true);
|
||||
|
||||
// Navigator.popAndPushNamed(context, loginRoute);
|
||||
return;
|
||||
}
|
||||
});
|
||||
return () {};
|
||||
}, []);
|
||||
|
||||
Future<void> getAddressFromLocation() async {
|
||||
try {
|
||||
isLoadingProsesCheckDistance.value = true;
|
||||
// Mendapatkan posisi pengguna
|
||||
LocationPermission permission = await Geolocator.requestPermission();
|
||||
|
||||
if (permission == LocationPermission.denied) {
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
SanckbarWidget(context, 'Izin lokasi ditolak', snackbarType.error);
|
||||
// Handle jika pengguna menolak izin lokasi
|
||||
print("Izin lokasi ditolak");
|
||||
return;
|
||||
}
|
||||
|
||||
Position position = await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high);
|
||||
|
||||
// Mendapatkan alamat dari posisi
|
||||
List<Placemark> placemarks = await placemarkFromCoordinates(
|
||||
position.latitude, position.longitude);
|
||||
|
||||
if (placemarks.isNotEmpty) {
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
Placemark placemark = placemarks.first;
|
||||
// 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();
|
||||
|
||||
// panggil check distance provider
|
||||
ref.read(checkDistanceProvider.notifier).checkDistance(
|
||||
selectedUser?.model.staffId ?? "",
|
||||
selectedUser?.model.companyId ?? "",
|
||||
positionLatitude.value,
|
||||
positionLongitude.value,
|
||||
);
|
||||
} else {
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
SanckbarWidget(
|
||||
context, 'Tidak dapat menemukan alamat.', snackbarType.error);
|
||||
print("Tidak dapat menemukan alamat.");
|
||||
}
|
||||
} catch (e) {
|
||||
print("Error: $e");
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
SanckbarWidget(context, 'Error : $e', snackbarType.error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> requestLocationPermission() async {
|
||||
var status = await Permission.location.request();
|
||||
isLoadingProsesCheckDistance.value = true;
|
||||
if (status.isGranted) {
|
||||
// Izin diberikan, lanjutkan dengan mendapatkan lokasi
|
||||
getAddressFromLocation();
|
||||
} else {
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
// Izin ditolak, berikan pemberitahuan atau instruksi
|
||||
// print('Izin lokasi ditolak');
|
||||
SanckbarWidget(context, 'Izin Ditolak', snackbarType.error);
|
||||
}
|
||||
}
|
||||
|
||||
// check distance provider
|
||||
ref.listen(checkDistanceProvider, (prev, next) {
|
||||
if (next is CheckDistanceStateLoading) {
|
||||
isLoadingProsesCheckDistance.value = true;
|
||||
} else if (next is CheckDistanceStateError) {
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
SanckbarWidget(context, next.message, snackbarType.warning);
|
||||
} else if (next is CheckDistanceStateDone) {
|
||||
isLoadingProsesCheckDistance.value = false;
|
||||
|
||||
if (varCurrentDistanceProvider?.selfie == "TRUE") {
|
||||
Navigator.pushNamed(context, presensiSelfieRoute);
|
||||
} else {
|
||||
Navigator.pushNamed(context, presensiRoute);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Scaffold(
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
|
||||
@@ -18,21 +136,33 @@ class HomeScreen extends HookConsumerWidget {
|
||||
width: Constant.getActualXPhone(context: context, x: 100),
|
||||
height: Constant.getActualYPhone(context: context, y: 100),
|
||||
child: FittedBox(
|
||||
child: FloatingActionButton(
|
||||
onPressed: () {},
|
||||
backgroundColor: Color(0xFFFFFFFF),
|
||||
shape: CircleBorder(),
|
||||
child: Container(
|
||||
width: Constant.getActualXPhone(context: context, x: 50),
|
||||
height: Constant.getActualYPhone(context: context, y: 50),
|
||||
decoration: BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage(
|
||||
'images/finger_tap_orange_botnav.png'), // Ganti dengan path gambar Anda
|
||||
child: (isLoadingProsesCheckDistance.value)
|
||||
? SizedBox(
|
||||
width: Constant.getActualXPhone(context: context, x: 50),
|
||||
height: Constant.getActualYPhone(context: context, y: 50),
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(
|
||||
color: Constant.textOrange,
|
||||
),
|
||||
),
|
||||
)
|
||||
: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
requestLocationPermission();
|
||||
},
|
||||
backgroundColor: Color(0xFFFFFFFF),
|
||||
shape: CircleBorder(),
|
||||
child: Container(
|
||||
width: Constant.getActualXPhone(context: context, x: 50),
|
||||
height: Constant.getActualYPhone(context: context, y: 50),
|
||||
decoration: BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage(
|
||||
'images/finger_tap_orange_botnav.png'), // Ganti dengan path gambar Anda
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
bottomNavigationBar: Container(
|
||||
@@ -102,61 +232,73 @@ class HomeScreen extends HookConsumerWidget {
|
||||
right: Constant.getActualXPhone(context: context, x: 27),
|
||||
),
|
||||
child: Container(
|
||||
child: ListTile(
|
||||
leading: Container(
|
||||
width:
|
||||
Constant.getActualXPhone(context: context, x: 36),
|
||||
height:
|
||||
Constant.getActualYPhone(context: context, y: 36),
|
||||
child: Image(
|
||||
image: AssetImage('images/avatar_c.png'),
|
||||
)),
|
||||
title: Text(
|
||||
"Stephen Kusumo",
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Constant.titleH1_700(context: context)
|
||||
..copyWith(
|
||||
color: Constant.textBlack,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
"Step@example.com",
|
||||
style:
|
||||
Constant.subtitle_500_12(context: context).copyWith(
|
||||
color: Constant.textLightGrey,
|
||||
),
|
||||
),
|
||||
trailing: Container(
|
||||
width:
|
||||
Constant.getActualXPhone(context: context, x: 36),
|
||||
height:
|
||||
Constant.getActualYPhone(context: context, y: 36),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
color: Colors.white,
|
||||
shape: BoxShape.rectangle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
offset: Offset(0, 12),
|
||||
blurRadius: 24,
|
||||
color: Color.fromRGBO(145, 158, 171, 0.12),
|
||||
child: (currentUserGoogle == null)
|
||||
? Center(
|
||||
child: CircularProgressIndicator(),
|
||||
)
|
||||
: ListTile(
|
||||
// leading: Container(
|
||||
// width: Constant.getActualXPhone(
|
||||
// context: context, x: 36),
|
||||
// height: Constant.getActualYPhone(
|
||||
// context: context, y: 36),
|
||||
// child: Image(
|
||||
// image: AssetImage('images/avatar_c.png'),
|
||||
// ),
|
||||
// ),
|
||||
leading: GoogleUserCircleAvatar(
|
||||
identity: currentUserGoogle,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: IconButton(
|
||||
onPressed: () {},
|
||||
icon: Container(
|
||||
width: Constant.getActualXPhone(
|
||||
context: context, x: 20),
|
||||
height: Constant.getActualYPhone(
|
||||
context: context, y: 20),
|
||||
child: Image(
|
||||
image: AssetImage('images/alert_badge.png'),
|
||||
title: Text(
|
||||
// "Stephen Kusumo",
|
||||
// selectedUser?.model.name ?? "",
|
||||
currentUserGoogle.displayName ?? "",
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Constant.titleH1_700(context: context)
|
||||
..copyWith(
|
||||
color: Constant.textBlack,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
// "Step@example.com",
|
||||
// currentUserGoogle?.email ?? "",
|
||||
selectedUser?.model.email ?? "",
|
||||
style: Constant.subtitle_500_12(context: context)
|
||||
.copyWith(
|
||||
color: Constant.textLightGrey,
|
||||
),
|
||||
),
|
||||
trailing: Container(
|
||||
width: Constant.getActualXPhone(
|
||||
context: context, x: 36),
|
||||
height: Constant.getActualYPhone(
|
||||
context: context, y: 36),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
color: Colors.white,
|
||||
shape: BoxShape.rectangle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
offset: Offset(0, 12),
|
||||
blurRadius: 24,
|
||||
color: Color.fromRGBO(145, 158, 171, 0.12),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: IconButton(
|
||||
onPressed: () {},
|
||||
icon: Container(
|
||||
width: Constant.getActualXPhone(
|
||||
context: context, x: 20),
|
||||
height: Constant.getActualYPhone(
|
||||
context: context, y: 20),
|
||||
child: Image(
|
||||
image: AssetImage('images/alert_badge.png'),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user