|
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:http/http.dart' as http;
- import 'package:barcode_scan2/barcode_scan2.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:permission_handler/permission_handler.dart' as pHandler;
-
-
- class Util{
-
- permissionCheck(context,pHandler.Permission permissionType,ifGranted,{customMessage=''})async{
- pHandler.PermissionStatus permission = await permissionType.status;
- if(permission!= pHandler.PermissionStatus.granted){
- if(permission== pHandler.PermissionStatus.denied || permission== pHandler.PermissionStatus.restricted){
- showToast('NORMAL', '${permissionType.toString().substring(permissionType.toString().lastIndexOf('.')+1)} permission is needed$customMessage. Please grant the permission!');
- await Future.delayed(Duration(seconds: 3));
- permission = await permissionType.request();
- }
- if(permission== pHandler.PermissionStatus.permanentlyDenied) {
- 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!');
- await Future.delayed(Duration(seconds: 3));
- pHandler.openAppSettings();
- SystemChannels.platform.invokeMethod('SystemNavigator.pop');
- }
- if(permission== pHandler.PermissionStatus.denied || permission== pHandler.PermissionStatus.restricted){
- showToast('NORMAL', '${permissionType.toString().substring(permissionType.toString().lastIndexOf('.')+1)} permission is needed$customMessage. Please grant the permission!');
- await Future.delayed(Duration(seconds: 3));
- permission = await permissionType.request();
- }
- await permissionCheck(context,permissionType,ifGranted);
- }
- else{
- await ifGranted();
- }
- }
-
- scan()async{
- String BarcodeText ='';
- try{
- ScanResult result = await BarcodeScanner.scan();
- BarcodeText = result.rawContent;
- return {'STATUS':1,'DATA':BarcodeText};
- }
- catch(e){
- return {'STATUS':0,'DATA':e};
- }
- }
-
- showLoadingFuture(context,future,{dismiss=false,onWillPop})async {
- var dialogContext;
- showDialog(
- context: context,
- builder: (BuildContext context) {
- dialogContext = context;
- return WillPopScope(
- onWillPop: onWillPop??()async{return true;},
- child: new Center(
- child: new CircularProgressIndicator(),
- ),
- );
- },
- barrierDismissible: dismiss,
- );
- var res = await future;
- Navigator.pop(dialogContext);
- return res;
- }
-
- JsonDataPostRaw(Map jsonData, String url,{timeout:false,duration:10}) async{
- const JsonDecoder decoder = const JsonDecoder();
- try {
- var headers = {'device':'mobile','Content-type': 'application/json'};
- var response;
- if (timeout)
- response = await http.post(
- Uri.parse(url), headers: headers,
- body: json.encode(jsonData)).timeout(
- Duration(seconds: duration));
- else
- response = await http.post(
- Uri.parse(url), headers: headers,
- body: json.encode(jsonData));
- final Map data = decoder.convert(response.body);
- return data;
- } on TimeoutException catch(e){
- return {"STATUS":"ERROR","ERROR":"Request Timeout"};
- }
- on Exception catch(exception){
- print(url);
- // Toast("Not Connected to Server", Colors.red);
- return {"STATUS":"ERROR","ERROR":"Not Connected to Server. $exception"};
- }
- }
-
- showToast(type,text)async{
- await Fluttertoast.cancel();
- Fluttertoast.showToast(
- msg: "$text",
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.BOTTOM,
- timeInSecForIosWeb: 1,
- backgroundColor: (type=='ERROR')?Colors.red:(type=='SUCCESS')?Colors.green:Colors.black38,
- textColor: Colors.white,
- fontSize: 16.0
- );
- }
- showLoading(context,{dissmissable=false,onwillpop}){
- showDialog(
- context: context,
- builder: (BuildContext context) {
- return WillPopScope(
- onWillPop: onwillpop??()async{return true;},
- child: new Center(
- child: new CircularProgressIndicator(),
- ),
- );
- },
- barrierDismissible: dissmissable,
- );
- }
-
- checkinternet()async{
- try {
- var result = await InternetAddress.lookup('google.com').timeout(Duration(seconds: 3),onTimeout: (){return null;});
- if (result!=null && result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
- return true;
- }
- else return false;
- } on SocketException catch (_) {
- return false;
- }
- }
-
- }
|