You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
11 KiB

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:supplier_new/common/preloader.dart';
const Color primaryColor = Color(0XFF8270DB);
const Color secondaryColor = Color(0XFFBFE0ED);
const Color greyColor = Color(0XFF7E7F80);
TextStyle fontTextStyle(double fontSize,Color fontColor,FontWeight weight) {
return GoogleFonts.inter(
textStyle: TextStyle(
fontSize: fontSize,
fontWeight: weight,
color:fontColor,
),
);
}
InputDecoration textFormFieldDecoration(IconData icon,var text){
return InputDecoration(
counterText: '',
filled: false,
fillColor: Colors.white,
/*prefixIcon: Icon(
icon,
color: Colors.white,
),*/
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(4.0),
borderSide: BorderSide(color: greyColor,
width: 1, )),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(4.0),
borderSide: BorderSide(color: Color(0XFF8270DB),width: 2,),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(4.0),
borderSide: BorderSide(color: greyColor),
),
labelText: text,
labelStyle:fontTextStyle(14,Color(0XFF7E7F80),FontWeight.w400),
/* TextStyle(color: greyColor, fontWeight: FontWeight.bold //<-- SEE HERE
),*/
);
}
TextStyle PreloaderText() {
return TextStyle(color:primaryColor);
}
class AppSettings{
static SharedPreferences sharedPreferences =
SharedPreferences.getInstance() as SharedPreferences;
static File? updatedImage;
static String image = '';
static String userName = '';
static String loginType = '';
static String accessType = '';
static String buildingName = '';
static String userAddress = '';
static String email = '';
static String phoneNumber = '';
static String accessToken = '';
static String customerId = '';
static double userLatitude = 0;
static double userLongitude = 0;
static String customerIdsign = '';
static String profileImage = '';
static String preloadText = 'Please wait';
static String host = 'http://armintaaqua.com:3000/api/';
static String loginUrl = host + 'supplierlogin';
// Shared preferences save,get and clear data
static saveData(String _key, _value, type) async {
sharedPreferences = await SharedPreferences.getInstance();
if (type == 'STRING') {
await sharedPreferences.setString(_key, _value.toString());
} else if (type == 'INTEGER') {
await sharedPreferences.setInt(_key, _value);
} else if (type == 'BOOL') {
await sharedPreferences.setBool(_key, _value);
} else if (type == 'DOUBLE') {
await sharedPreferences.setDouble(_key, _value);
}
}
static getData(String _key, type) async {
sharedPreferences = await SharedPreferences.getInstance();
if (type == 'STRING') {
return sharedPreferences.getString(_key) ?? '';
} else if (type == 'INTEGER') {
return sharedPreferences.getInt(_key) ?? -1;
} else if (type == 'BOOL') {
return sharedPreferences.getBool(_key) ?? -1;
} else if (type == 'DOUBLE') {
return sharedPreferences.getDouble(_key) ?? -1;
}
}
static clearSharedPreferences() async {
sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.clear();
}
/* Preloader */
static GlobalKey<State> preLoaderKey = new GlobalKey<State>();
static Future<void> preLoaderDialog(BuildContext context) async {
try {
preLoaderKey = new GlobalKey<State>();
Dialogs.showLoadingDialog(context, preLoaderKey);
} catch (error) {}
}
static Future<bool> internetConnectivity() async {
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
return true;
}
} on SocketException catch (_) {
return false;
}
return false;
}
/*Apis Starts here*/
static Future<bool> login(payload) async {
var response = await http.post(Uri.parse(loginUrl),
body: json.encode(payload),
headers: {'Content-type': 'application/json'});
if (response.statusCode == 200) {
try {
var _response = json.decode(response.body);
print(_response['simplydata']['error']);
if (_response['simplydata']['error'] == false) {
await saveAvailableReportAndLocationsInMemory(_response);
//await saveProfilePic(_response);
return true;
} else {
return false;
}
} catch (e) {
// display error toast
return false;
}
} else {
return false;
}
}
/*Apis ends here*/
//save data local
static Future<void> saveAvailableReportAndLocationsInMemory(
dynamic input) async {
// save login name information
await saveData('username', input['simplydata']['username'], 'STRING');
await saveData('logintype', input['simplydata']['loginType'], 'STRING');
await saveData('all_motor_access', input['simplydata']['all_motor_access'], 'STRING');
await saveData('buildingname', input['simplydata']['buildingName'], 'STRING');
await saveData(
'access_token', input['simplydata']['access_token'], 'STRING');
await saveData('phone', input['simplydata']['phone'], 'STRING');
await saveData('email', input['simplydata']['email'][0]['email'], 'STRING');
await saveData('customerId', input['simplydata']['customerId'], 'STRING');
await saveData('profile', input['simplydata']['picture'], 'STRING');
await saveData('user_address', input['simplydata']['address1'], 'STRING');
if(input['simplydata']['latitude']==0){
input['simplydata']['latitude']=0.0;
}
if(input['simplydata']['longitude']==0){
input['simplydata']['longitude']=0.0;
}
await saveData('latitude', input['simplydata']['latitude'], 'DOUBLE');
await saveData('longitude', input['simplydata']['longitude'], 'DOUBLE');
await saveData('fcmId', input['simplydata']['fcmId'], 'STRING');
//latitude,longitude
await loadDataFromMemory();
}
static Future<void> saveProfile(dynamic image) async {
// save login name information
await saveData('profile', image.toString(), 'STRING');
//await loadDataFromMemory();
}
static Uint8List convertBase64Image(String base64String) {
return Base64Decoder().convert(base64String.split(',').last);
}
static Future<void> loadDataFromMemory() async {
userName = await getData('username', 'STRING');
loginType = await getData('logintype', 'STRING');
accessType = await getData('all_motor_access', 'STRING');
buildingName = await getData('buildingname', 'STRING');
accessToken = await getData('access_token', 'STRING');
email = await getData('email', 'STRING');
userAddress = await getData('user_address', 'STRING');
phoneNumber = await getData('phone', 'STRING');
customerId = await getData('customerId', 'STRING');
userLatitude = await getData('latitude', 'DOUBLE');
userLongitude = await getData('longitude', 'DOUBLE');
//profilePictureUrl = await getData('profile', 'STRING');
// fcmId = await getData('fcmId', 'STRING');
//profileImage=await getData('profile', 'STRING');
getProfile();
}
static String getStringFromBytes(var data) {
final buffer = data.buffer;
var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
var bufferdata = utf8.decode(list);
String bs4str = base64.encode(list);
Uint8List decodedbytes = base64.decode(bs4str);
//OR
// String bs4str1 = base64Encode(imgbytes);
String s = new String.fromCharCodes(list);
var outputAsUint8List = new Uint8List.fromList(s.codeUnits);
return bufferdata;
}
static Uint8List image1(dynamicList) {
List<int> intList =
dynamicList.cast<int>().toList(); //This is the magical line.
Uint8List data = Uint8List.fromList(intList);
return data;
}
static Future<void> getProfile() async {
final image1 = await getData('profile', 'STRING');
//Io.File.fromUri(imageFile.uri)
var x = image1;
if (image1 == null) return;
if (image1 == '') {
updatedImage = null;
} else if (image1 == 'null') {
updatedImage = null;
} else {
final imageTemp = File(image1);
updatedImage = imageTemp;
}
print(updatedImage);
}
//assign saved values to variables
static Future<void> saveProfilePic(dynamic input) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
// your custom int list
List<dynamic> mList = input['simplydata']['picture']['data'];
print(mList.length);
//input['simplydata']['picture']['data'];[0,1,2,3]
// convert your custom list to string list
List<String> stringsList = mList.map((i) => i.toString()).toList();
// store your string list in shared prefs
prefs.setStringList("stringList", stringsList);
// String bar = utf8.decode(bytes);
//String convertedValue = utf8.decode(mList);
}
static void longFailedToast(String message) {
Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
static void longSuccessToast(String message) {
Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.green,
textColor: Colors.white,
fontSize: 16.0);
}
static appBar(String title) {
title = title ?? '';
return AppBar(
backgroundColor: Colors.white,
elevation: 0,
scrolledUnderElevation: 0,
title: Text(title,style: fontTextStyle(14,Color(0XFF2A2A2A),FontWeight.w500),),
iconTheme: IconThemeData(color: Color(0XFF2A2A2A)),
actions: [
Row(
children: [
Padding(padding: EdgeInsets.fromLTRB(10,10,0,10),
child: IconButton(
icon: Image(
image: AssetImage('images/customercare_appbar.png')
),
onPressed: (){
},
),
),
Padding(padding: EdgeInsets.fromLTRB(0,10,10,10),
child: IconButton(
icon: Image.asset(
'images/notification_appbar.png', // Example URL image
),
onPressed: (){
},
),
)
],
)
],
);
}
}