Flutter app for Asset Management
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

137 строки
4.7 KiB

  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:http/http.dart' as http;
  7. import 'package:barcode_scan2/barcode_scan2.dart';
  8. import 'package:fluttertoast/fluttertoast.dart';
  9. import 'package:permission_handler/permission_handler.dart' as pHandler;
  10. class Util{
  11. permissionCheck(context,pHandler.Permission permissionType,ifGranted,{customMessage=''})async{
  12. pHandler.PermissionStatus permission = await permissionType.status;
  13. if(permission!= pHandler.PermissionStatus.granted){
  14. if(permission== pHandler.PermissionStatus.denied || permission== pHandler.PermissionStatus.restricted){
  15. showToast('NORMAL', '${permissionType.toString().substring(permissionType.toString().lastIndexOf('.')+1)} permission is needed$customMessage. Please grant the permission!');
  16. await Future.delayed(Duration(seconds: 3));
  17. permission = await permissionType.request();
  18. }
  19. if(permission== pHandler.PermissionStatus.permanentlyDenied) {
  20. showToast('ERROR', 'It seems, your system security explicitly denied access to your ${permissionType.toString().substring(permissionType.toString().lastIndexOf('.')+1)} permission. Please MANUALLY enable it in setings!');
  21. await Future.delayed(Duration(seconds: 3));
  22. pHandler.openAppSettings();
  23. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  24. }
  25. if(permission== pHandler.PermissionStatus.denied || permission== pHandler.PermissionStatus.restricted){
  26. showToast('NORMAL', '${permissionType.toString().substring(permissionType.toString().lastIndexOf('.')+1)} permission is needed$customMessage. Please grant the permission!');
  27. await Future.delayed(Duration(seconds: 3));
  28. permission = await permissionType.request();
  29. }
  30. await permissionCheck(context,permissionType,ifGranted);
  31. }
  32. else{
  33. await ifGranted();
  34. }
  35. }
  36. scan()async{
  37. String BarcodeText ='';
  38. try{
  39. ScanResult result = await BarcodeScanner.scan();
  40. BarcodeText = result.rawContent;
  41. return {'STATUS':1,'DATA':BarcodeText};
  42. }
  43. catch(e){
  44. return {'STATUS':0,'DATA':e};
  45. }
  46. }
  47. showLoadingFuture(context,future,{dismiss=false,onWillPop})async {
  48. var dialogContext;
  49. showDialog(
  50. context: context,
  51. builder: (BuildContext context) {
  52. dialogContext = context;
  53. return WillPopScope(
  54. onWillPop: onWillPop??()async{return true;},
  55. child: new Center(
  56. child: new CircularProgressIndicator(),
  57. ),
  58. );
  59. },
  60. barrierDismissible: dismiss,
  61. );
  62. var res = await future;
  63. Navigator.pop(dialogContext);
  64. return res;
  65. }
  66. JsonDataPostRaw(Map jsonData, String url,{timeout:false,duration:10}) async{
  67. const JsonDecoder decoder = const JsonDecoder();
  68. try {
  69. var headers = {'device':'mobile','Content-type': 'application/json'};
  70. var response;
  71. if (timeout)
  72. response = await http.post(
  73. Uri.parse(url), headers: headers,
  74. body: json.encode(jsonData)).timeout(
  75. Duration(seconds: duration));
  76. else
  77. response = await http.post(
  78. Uri.parse(url), headers: headers,
  79. body: json.encode(jsonData));
  80. final Map data = decoder.convert(response.body);
  81. return data;
  82. } on TimeoutException catch(e){
  83. return {"STATUS":"ERROR","ERROR":"Request Timeout"};
  84. }
  85. on Exception catch(exception){
  86. print(url);
  87. // Toast("Not Connected to Server", Colors.red);
  88. return {"STATUS":"ERROR","ERROR":"Not Connected to Server. $exception"};
  89. }
  90. }
  91. showToast(type,text)async{
  92. await Fluttertoast.cancel();
  93. Fluttertoast.showToast(
  94. msg: "$text",
  95. toastLength: Toast.LENGTH_SHORT,
  96. gravity: ToastGravity.BOTTOM,
  97. timeInSecForIosWeb: 1,
  98. backgroundColor: (type=='ERROR')?Colors.red:(type=='SUCCESS')?Colors.green:Colors.black38,
  99. textColor: Colors.white,
  100. fontSize: 16.0
  101. );
  102. }
  103. showLoading(context,{dissmissable=false,onwillpop}){
  104. showDialog(
  105. context: context,
  106. builder: (BuildContext context) {
  107. return WillPopScope(
  108. onWillPop: onwillpop??()async{return true;},
  109. child: new Center(
  110. child: new CircularProgressIndicator(),
  111. ),
  112. );
  113. },
  114. barrierDismissible: dissmissable,
  115. );
  116. }
  117. checkinternet()async{
  118. try {
  119. var result = await InternetAddress.lookup('google.com').timeout(Duration(seconds: 3),onTimeout: (){return null;});
  120. if (result!=null && result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
  121. return true;
  122. }
  123. else return false;
  124. } on SocketException catch (_) {
  125. return false;
  126. }
  127. }
  128. }