|
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:http/http.dart' as http;
- import 'package:flutter/material.dart';
- import 'package:flushbar/flushbar.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{
- JsonDataPostRaw(Map jsonData, String url,{timeout:false}) async{
- const JsonDecoder decoder = const JsonDecoder();
- try {
- var response;
- if (timeout)
- response = await http.post(
- '$url', headers: {'Content-type': 'application/json'},
- body: json.encode(jsonData)).timeout(
- const Duration(seconds: 10));
- else
- response = await http.post(
- '$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 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,
- );
- }
-
- showFlushbar(context,text,{color:Colors.grey}){
- Flushbar(
- message: "$text",
- backgroundColor: color,
- duration: Duration(seconds: 5),
- )..show(context);
- }
- streamLocation(context)async{
- print('checking location');
- bool gpsEnabled = false;
- pHandler.PermissionStatus permissionEnabled = await pHandler.Permission.locationWhenInUse.status;
- 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(permissionEnabled == pHandler.PermissionStatus.granted){
- if(locationStream==null){
- locationStream = location.onLocationChanged.listen((LocationData event) {
- currentPosisiton = event;
- });
- }
- }
- else if(permissionEnabled==pHandler.PermissionStatus.denied || permissionEnabled==pHandler.PermissionStatus.undetermined){
- print('requesting permission');
- showFlushbar(context,'Location permission is needed to locate your REAL location. Please grant the permission!');
- await Future.delayed(Duration(seconds: 1));
- await pHandler.Permission.locationWhenInUse.request();
- await streamLocation(context);
- }
- else if (permissionEnabled==pHandler.PermissionStatus.permanentlyDenied){
- showFlushbar(context,'It seems, your system security explicitly denied the permission to access your location. Please MANUALLY enable it in setings!');
- await Future.delayed(Duration(seconds: 3));
- AppSettings.openAppSettings();
- exit(0);
- }
- }
- }
- }
|