flutter app untuk unitstock
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

121 rivejä
4.6 KiB

  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/services.dart';
  5. import 'package:http/http.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flushbar/flushbar.dart';
  8. import 'package:http/io_client.dart';
  9. import '../main.dart';
  10. import 'package:location/location.dart';
  11. import 'package:permission_handler/permission_handler.dart' as pHandler;
  12. // import 'package:app_settings/app_settings.dart';
  13. class Util{
  14. bool useLocal = false;
  15. JsonDataPostRaw(Map jsonData, String url,{timeout:false}) async{
  16. const JsonDecoder decoder = const JsonDecoder();
  17. try {
  18. var response;
  19. if (timeout)
  20. response = await http.post(
  21. Uri.parse(url), headers: {'Content-type': 'application/json'},
  22. body: json.encode(jsonData)).timeout(
  23. const Duration(seconds: 10));
  24. else
  25. response = await http.post(
  26. Uri.parse(url), headers: {'Content-type': 'application/json'},
  27. body: json.encode(jsonData));
  28. final Map data = decoder.convert(response.body);
  29. return data;
  30. } on TimeoutException catch(e){
  31. return {"STATUS":0,"DATA":"Request Timeout"};
  32. }
  33. on HandshakeException catch(e){
  34. if(useLocal){
  35. return {"STATUS":0,"DATA":"Not Connected to Server. $e"};
  36. }
  37. else{
  38. useLocal = true;
  39. http = IOClient(HttpClient(context: clientContext));
  40. return await JsonDataPostRaw(jsonData, url,timeout:timeout);
  41. }
  42. }
  43. on Exception catch(exception){
  44. print(url);
  45. // Toast("Not Connected to Server", Colors.red);
  46. return {"STATUS":0,"DATA":"Not Connected to Server. $exception"};
  47. }
  48. }
  49. showLoading(context,{dissmissable=false,onwillpop}){
  50. showDialog(
  51. context: context,
  52. builder: (BuildContext context) {
  53. return WillPopScope(
  54. onWillPop: onwillpop??()async{return true;},
  55. child: new Center(
  56. child: new CircularProgressIndicator(),
  57. ),
  58. );
  59. },
  60. barrierDismissible: dissmissable,
  61. );
  62. }
  63. permissionCheck(context,pHandler.Permission permissionType,ifGranted,{customMessage=''})async{
  64. pHandler.PermissionStatus permission = await permissionType.status;
  65. if(permission!= pHandler.PermissionStatus.granted){
  66. if(permission== pHandler.PermissionStatus.denied || permission== pHandler.PermissionStatus.restricted){
  67. showFlushbar(context,'${permissionType.toString().substring(permissionType.toString().lastIndexOf('.')+1)} permission is needed$customMessage. Please grant the permission!');
  68. await Future.delayed(Duration(seconds: 3));
  69. permission = await permissionType.request();
  70. }
  71. if(permission== pHandler.PermissionStatus.permanentlyDenied) {
  72. showFlushbar(context,'It seems, your system security explicitly denied access to your ${permissionType.toString().substring(permissionType.toString().lastIndexOf('.')+1)} permission. Please MANUALLY enable it in setings!');
  73. await Future.delayed(Duration(seconds: 3));
  74. pHandler.openAppSettings();
  75. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  76. }
  77. if(permission== pHandler.PermissionStatus.denied || permission== pHandler.PermissionStatus.restricted){
  78. showFlushbar(context,'${permissionType.toString().substring(permissionType.toString().lastIndexOf('.')+1)} permission is needed$customMessage. Please grant the permission!');
  79. await Future.delayed(Duration(seconds: 3));
  80. permission = await permissionType.request();
  81. }
  82. await permissionCheck(context,permissionType,ifGranted);
  83. }
  84. else{
  85. await ifGranted();
  86. }
  87. }
  88. showFlushbar(context,text,{color:Colors.grey}){
  89. Flushbar(
  90. message: "$text",
  91. backgroundColor: color,
  92. duration: Duration(seconds: 5),
  93. )..show(context);
  94. }
  95. streamLocation(context)async{
  96. print('checking location');
  97. await permissionCheck(context,pHandler.Permission.locationWhenInUse,()async{
  98. location.changeSettings(accuracy: LocationAccuracy.high);
  99. bool gpsEnabled = false;
  100. if(await location.serviceEnabled()){
  101. gpsEnabled = await location.serviceEnabled();
  102. }
  103. else{
  104. print('requesting gps');
  105. gpsEnabled = await location.requestService();
  106. await streamLocation(context);
  107. }
  108. // print([gpsEnabled,permissionEnabled]);
  109. if(gpsEnabled){
  110. if(locationStream==null){
  111. locationStream = location.onLocationChanged.listen((LocationData event) {
  112. currentPosisiton = event;
  113. });
  114. }
  115. }
  116. },customMessage: " to locate your REAL location");
  117. }
  118. }