import 'dart:async'; import 'dart:convert'; import 'dart:ffi'; import 'package:employee_selfservice_mobile/Screens/Menu/Canvasing/filing_screen.dart'; import 'package:employee_selfservice_mobile/Screens/Menu/Canvasing/navigate_post.dart'; import 'package:employee_selfservice_mobile/Screens/Menu/Canvasing/startFiling_post.dart'; import 'package:employee_selfservice_mobile/Screens/Menu/Canvasing/updateGPS_post.dart'; import 'package:flutter/material.dart'; import 'package:flutter_map/plugin_api.dart'; import 'package:geolocator/geolocator.dart'; import 'package:latlong2/latlong.dart' as latlong; import 'package:location/location.dart' as loc; import 'dart:developer' as logDev; import 'package:progress_dialog_null_safe/progress_dialog_null_safe.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sliding_up_panel/sliding_up_panel.dart'; class CanvasingDetail extends StatefulWidget { const CanvasingDetail({Key? key}) : super(key: key); @override State createState() => _CanvasingDetail(); } class _CanvasingDetail extends State { late latlong.LatLng point = latlong.LatLng(-6.183580253674716, 106.93103973792664); late Position position; MapController _mapController = MapController(); @override void initState() { determinePosition(); WidgetsBinding.instance.addPostFrameCallback((_) { initTimer(); }); super.initState(); } Timer? timer; void initTimer(){ if (timer != null && timer!.isActive) return; timer = Timer.periodic(Duration(seconds: 10), (timer) { setState(() { updatePosition(); }); }); } @override void dispose() { timer?.cancel(); super.dispose(); } determinePosition() async { StreamSubscription serviceStatusStream = Geolocator.getServiceStatusStream().listen( (ServiceStatus status) { print(status); }); ProgressDialog loading = ProgressDialog(context); loading = ProgressDialog(context, type: ProgressDialogType.normal, isDismissible: false, showLogs: true); loading.style( message: 'Please Wait .....', borderRadius: 3, backgroundColor: Colors.white, progressWidget: CircularProgressIndicator(), elevation: 10.0, padding: EdgeInsets.all(10), insetAnimCurve: Curves.easeInOut, progress: 0.0, maxProgress: 100.0, progressTextStyle: TextStyle(color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w400), messageTextStyle: TextStyle(color: Colors.black, fontSize: 15.0, fontWeight: FontWeight.w600)); await loading.show(); bool serviceEnabled = await Geolocator.isLocationServiceEnabled(); logDev.log(serviceEnabled.toString(), name: "ON/OFF LOCATION SERVICE"); if (!serviceEnabled) { return Future.error('Location services are disabled.'); } LocationPermission permission = await Geolocator.checkPermission(); if (permission == LocationPermission.deniedForever) { await loading.hide(); //return Future.error('Location permissions are permanently denied, we cannot request permissions.'); return alertDialogPermissionDeniedForever(context); } else if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission != LocationPermission.whileInUse && permission != LocationPermission.always) { await loading.hide(); //return Future.error('Location permissions are denied (actual value: $permission).'); return alertDialogPermissionDenied(context); } } final SharedPreferences prefs = await SharedPreferences.getInstance(); String? idDetail = prefs.getString('idDetail'); int? user_id = prefs.getInt('user_id'); Navigate_Post.connectToAPI(idDetail!, user_id!).then((valueResult) async { Map object = json.decode(valueResult); if (object.containsKey("result").toString() == "true") { String result = object['result'].toString(); String status = object['result']['status'].toString(); logDev.log(result, name: "Status??"); if (status.contains("success")){ String mapsPoint = object['result']['maps'].toString(); double? lat = double.tryParse(mapsPoint.split(",")[0]); double? long = double.tryParse(mapsPoint.split(",")[1]); logDev.log(mapsPoint, name: "titik lokasinya"); position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); logDev.log(position.toString(), name: "Position GEOLOCATOR"); if (position.isMocked) { await loading.hide(); setState(() { alertDialogFakeGPSDetected(context); }); } else { setState(() { point = latlong.LatLng(lat!,long!); logDev.log(point.toString(), name: "POINT GEOLOCATOR"); _mapController.move(point, 18); }); } await loading.hide(); await loading.hide(); } else if (status.contains("failed")){ setState(() { alertDialogFailedResponse(context); }); await loading.hide(); } } else { setState(() { alertDialogFailedResponse(context); }); await loading.hide(); } await loading.hide(); }); logDev.log("DAH SELESE BUiLD NAVIGATENYA!", name: "UDAH?"); } Future updatePosition() async { logDev.log("NAH BARU MULAI!", name: "MULAI?"); ProgressDialog loading = ProgressDialog(context); loading = ProgressDialog(context, type: ProgressDialogType.normal, isDismissible: false, showLogs: true); loading.style( message: 'Please Wait .....', borderRadius: 3, backgroundColor: Colors.white, progressWidget: CircularProgressIndicator(), elevation: 10.0, padding: EdgeInsets.all(10), insetAnimCurve: Curves.easeInOut, progress: 0.0, maxProgress: 100.0, progressTextStyle: TextStyle( color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w400), messageTextStyle: TextStyle( color: Colors.black, fontSize: 15.0, fontWeight: FontWeight.w600)); bool serviceEnabled; LocationPermission permission; serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { await loading.hide(); return Future.error('Location services are disabled.'); } permission = await Geolocator.checkPermission(); if (permission == LocationPermission.deniedForever) { await loading.hide(); return alertDialogPermissionDeniedForever(context); } else if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission != LocationPermission.whileInUse && permission != LocationPermission.always) { await loading.hide(); return alertDialogPermissionDenied(context); } } position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); if (position.isMocked) { await loading.hide(); setState(() { alertDialogFakeGPSDetected(context); }); } else { await loading.hide(); final SharedPreferences prefs = await SharedPreferences.getInstance(); String? idDetail = prefs.getString('idDetail'); int? user_id = prefs.getInt('user_id'); point = latlong.LatLng(position.latitude, position.longitude); logDev.log(point.toString(), name: "UPDATE POINT GEOLOCATOR"); _mapController.move(point, 18); position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); String titik = position.latitude.toString() + "," + position.longitude.toString(); logDev.log(titik, name: "TITIK UPDATE"); setState(() { UpdateGPS_Post.connectToAPI(idDetail!, titik, user_id!).then((valueResult) async { Map object = json.decode(valueResult); if (object.containsKey("result").toString() == "true") { String status = object['result']['status'].toString(); if (status.contains("success")){ String mapsPoint = object['result']['maps'].toString(); logDev.log(mapsPoint, name: "titik lokasinya"); position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); logDev.log(position.toString(), name: "Position GEOLOCATOR"); if (position.isMocked) { await loading.hide(); setState(() { alertDialogFakeGPSDetected(context); }); } else { setState(() { point = latlong.LatLng(double.parse(mapsPoint.split(",")[0]), double.parse(mapsPoint.split(",")[1])); logDev.log(point.toString(), name: "POINT GEOLOCATOR"); _mapController.move(point, 18); }); } await loading.hide(); } else if (status.contains("failed")){ setState(() { alertDialogFailedResponse(context); }); await loading.hide(); } } else { setState(() { alertDialogFailedResponse(context); }); await loading.hide(); } await loading.hide(); }); }); } } PanelController _panelController = PanelController(); void togglePanel() => _panelController.isPanelOpen ? _panelController.close() : _panelController.open(); @override Widget build(BuildContext context) { var size = MediaQuery.of(context).size; return Scaffold( body: SlidingUpPanel( controller: _panelController, parallaxEnabled: true, maxHeight: size.height * 0.2, minHeight: size.height * 0.2, body: Container ( child: Stack( children: [ FlutterMap( mapController: _mapController, options: new MapOptions( onTap: (p, point) async {}, center: point, zoom: 15), children: [ TileLayer( minZoom: 1, maxZoom: 22, maxNativeZoom: 18, minNativeZoom: 1, backgroundColor: Colors.black54, urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', subdomains: ['a', 'b', 'c'], ), MarkerLayer(markers: [ Marker( width: 135, height: 135, point: point, builder: (ctx) => Container( child: Icon( Icons.location_on, color: Colors.red, ), )) ]) ], ), Container( alignment: AlignmentDirectional.bottomEnd, child: Container( margin: EdgeInsets.fromLTRB(10, 15, 0, (size.height*0.3) + 15), child: RawMaterialButton( onPressed: () { setState(() { updatePosition(); }); }, elevation: 5.0, fillColor: Colors.white, child: Image.asset('assets/images/location1.png', height: 40, width: 40), padding: EdgeInsets.all(5.0), shape: CircleBorder(), ), ), ), ], ), ), panel: Center( child: Column( children: [ GestureDetector( onTap: togglePanel, child: Center( child: Container( margin: EdgeInsets.only(top: 15), height: 5, width: size.width * 0.1, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.grey ), ), ), ), Container( width: double.infinity, margin: EdgeInsets.fromLTRB(10, 10, 10, 10), //decoration: BoxDecoration(border: Border.all(color: Colors.black), borderRadius: BorderRadius.all(Radius.circular(15))), child: Column( children: [ ], ), ), Container( margin: EdgeInsets.fromLTRB(10, 15, 10, 15), child: InkWell( child: Container( padding: EdgeInsets.fromLTRB(10, 10, 10, 10), width: double.infinity, decoration: BoxDecoration( borderRadius: BorderRadius.circular(5), gradient: LinearGradient(colors: [ Color(0xFF2D4059), Color(0xFF2D4059), ])), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Start Filing\t\t', textAlign: TextAlign.center, style: TextStyle( color: Colors.white, fontSize: 17, fontWeight: FontWeight.w500)), Image.asset( 'assets/images/ic_arrow_white.png', width: 30, height: 30, ) ], )), onTap: () async { final SharedPreferences prefs = await SharedPreferences.getInstance(); String? idDetail = prefs.getString('idDetail'); int? user_id = prefs.getInt('user_id'); StartFiling_Post.connectToAPI(idDetail!, user_id!).then((valueResult) async { Navigator.push(context, MaterialPageRoute(builder: (context) => FilingScreen())); }); }, ), ) ], ), ), borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15)), ), ); } } alertDialogFailedRetrievedData(BuildContext context) { Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => CanvasingDetail())); }, ); Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text("Failed to Retrieve Data"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); } alertDialogFailedResponse(BuildContext context) { Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => CanvasingDetail())); }, ); Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text("Server Response Error"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); } alertDialogFakeGPSDetected(BuildContext context) { Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => CanvasingDetail())); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text("Fake GPS Detected, Please Use Default GPS!"), actions: [ okButton, ], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); } alertDialogPermissionDenied(BuildContext context) { Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => CanvasingDetail())); }, ); Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text("Location permissions are denied, we cannot request permissions"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); } alertDialogPermissionDeniedForever(BuildContext context) { Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text( "Location permissions are permanently denied, we cannot request permissions. Please configure it in your device settings."), actions: [noButton], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); } alertDialogEnableLocaionServices(BuildContext context) { ProgressDialog loading = ProgressDialog(context); loading = ProgressDialog(context, type: ProgressDialogType.normal, isDismissible: false, showLogs: true); loading.style( message: 'Please Wait .....', borderRadius: 3, backgroundColor: Colors.white, progressWidget: CircularProgressIndicator(), elevation: 10.0, padding: EdgeInsets.all(10), insetAnimCurve: Curves.easeInOut, progress: 0.0, maxProgress: 100.0, progressTextStyle: TextStyle( color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w400), messageTextStyle: TextStyle( color: Colors.black, fontSize: 15.0, fontWeight: FontWeight.w600)); Widget okButton = TextButton( child: Text("Open Settings"), onPressed: () { //AppSettings.openLocationSettings(); Navigator.of(context, rootNavigator: true).pop(); loading.hide(); }, ); Widget noButton = TextButton( child: Text("No, thanks"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); loading.hide(); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text("Employee Self Service"), content: Text("For a better experience, please turn on your device location"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); }