flutter app untuk unitstock
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

96 lignes
3.3 KiB

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