|
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:flutter/services.dart';
- import 'package:http/http.dart';
- import 'package:flutter/material.dart';
- import 'package:flushbar/flushbar.dart';
- import 'package:http/io_client.dart';
- import '../main.dart';
- import 'package:location/location.dart';
- import 'package:permission_handler/permission_handler.dart' as pHandler;
- // import 'package:app_settings/app_settings.dart';
-
- class Util{
- bool useLocal = false;
- JsonDataPostRaw(Map jsonData, String url,{timeout:false}) async{
- const JsonDecoder decoder = const JsonDecoder();
- try {
- var response;
- if (timeout)
- response = await http.post(
- Uri.parse(url), headers: {'Content-type': 'application/json'},
- body: json.encode(jsonData)).timeout(
- const Duration(seconds: 10));
- else
- response = await http.post(
- Uri.parse(url), headers: {'Content-type': 'application/json'},
- body: json.encode(jsonData));
- final Map data = decoder.convert(response.body);
- return data;
- } on TimeoutException catch(e){
- return {"STATUS":0,"DATA":"Request Timeout"};
- }
- on HandshakeException catch(e){
- if(useLocal){
- return {"STATUS":0,"DATA":"Not Connected to Server. $e"};
- }
- else{
- useLocal = true;
- http = IOClient(HttpClient(context: clientContext));
- return await JsonDataPostRaw(jsonData, url,timeout:timeout);
- }
-
- }
- on Exception catch(exception){
- print(url);
- // Toast("Not Connected to Server", Colors.red);
- return {"STATUS":0,"DATA":"Not Connected to Server. $exception"};
- }
- }
-
- 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,
- );
- }
- 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){
- showFlushbar(context,'${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) {
- 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!');
- await Future.delayed(Duration(seconds: 3));
- pHandler.openAppSettings();
- SystemChannels.platform.invokeMethod('SystemNavigator.pop');
- }
- if(permission== pHandler.PermissionStatus.denied || permission== pHandler.PermissionStatus.restricted){
- showFlushbar(context,'${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();
- }
- }
- showFlushbar(context,text,{color:Colors.grey}){
- Flushbar(
- message: "$text",
- backgroundColor: color,
- duration: Duration(seconds: 5),
- )..show(context);
- }
- streamLocation(context)async{
- print('checking location');
- await permissionCheck(context,pHandler.Permission.locationWhenInUse,()async{
- location.changeSettings(accuracy: LocationAccuracy.high);
- bool gpsEnabled = false;
- if(await location.serviceEnabled()){
- gpsEnabled = await location.serviceEnabled();
- }
- else{
- print('requesting gps');
- gpsEnabled = await location.requestService();
- await streamLocation(context);
- }
- // print([gpsEnabled,permissionEnabled]);
- if(gpsEnabled){
- if(locationStream==null){
- locationStream = location.onLocationChanged.listen((LocationData event) {
- currentPosisiton = event;
- });
- }
- }
- },customMessage: " to locate your REAL location");
- }
- }
|