You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PatientApp-KKUMC/lib/uitl/utils.dart

156 lines
4.1 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'app_shared_preferences.dart';
import 'app_toast.dart';
AppSharedPreferences sharedPref = new AppSharedPreferences();
class Utils {
// static ProgressDialog pr;
///show custom Error Toast
/// [message] to show for user
static showErrorToast([String message]) {
String localMsg = generateContactAdminMessage();
if (message != null) {
localMsg = message.toString();
}
AppToast.showErrorToast(message: localMsg);
}
/// Check The Internet Connection
static Future<bool> checkConnection() async {
ConnectivityResult connectivityResult =
await (Connectivity().checkConnectivity());
if ((connectivityResult == ConnectivityResult.mobile) ||
(connectivityResult == ConnectivityResult.wifi)) {
return true;
} else {
return false;
}
}
/// generate Contact Admin Message
static generateContactAdminMessage([err]) {
String localMsg = 'Something wrong happened, please contact the admin';
if (err != null) {
localMsg = localMsg + '\n \n' + err.toString();
}
return localMsg;
}
/// hides the keyboard if its already open
static hideKeyboard(BuildContext context) {
FocusScope.of(context).unfocus();
}
bool isSAUDIIDValid(String id, type) {
if (type == 1) {
if (id == null) {
return false;
}
try {
id = id.toString();
id = id.trim();
var returnValue = int.parse(id);
var sum = 0;
if (returnValue > 0) {
var type = int.parse(id[0]);
if (id.length != 10) {
return false;
}
if (type != 2 && type != 1) {
return false;
}
for (var i = 0; i < 10; i++) {
if (i % 2 == 0) {
var a = id[i];
var x = int.parse(a) * 2;
var b = x.toString();
if (b.length == 1) {
b = "0" + b;
}
sum += int.parse(b[0]) + int.parse(b[1]);
} else {
sum += int.parse(id[i]);
}
}
return sum % 10 == 0;
}
} catch (err) {}
return false;
} else {
return true;
}
}
static String getAppointmentTransID(int projectID, int clinicID, int appoNo) {
return projectID.toString() +
'-' +
clinicID.toString() +
'-' +
appoNo.toString();
}
static String getAdvancePaymentTransID(int projectID, int fileNumber) {
return projectID.toString() +
'-' +
fileNumber.toString() +
'-' +
DateTime.now().millisecondsSinceEpoch.toString();
}
bool validateIDBox(String value, type) {
Pattern pattern = loginIDPattern(type); //r'^\d+(?:\.\d+)?$';
RegExp regex = new RegExp(pattern);
return regex.hasMatch(value);
}
String loginIDPattern(loginType) {
var length = loginType == 1 ? 10 : 7;
return "([0-9]{" + length.toString() + "})";
}
static showProgressDialog(context, [String message = "Loading..."]) async {
// pr = ProgressDialog(context,
// type: ProgressDialogType.Normal, isDismissible: false, showLogs: false);
// pr.style(
// message: message,
// borderRadius: 10.0,
// backgroundColor: Colors.white,
// elevation: 10.0,
// insetAnimCurve: Curves.easeInOut,
// progress: 0.0,
// maxProgress: 100.0,
// progressTextStyle: TextStyle(
// color: Colors.black, fontSize: 13.0, fontWeight: FontWeight.w400),
// messageTextStyle: TextStyle(
// color: Colors.black, fontSize: 19.0, fontWeight: FontWeight.w600));
// if (!pr.isShowing()) {
// await pr.show();
// } else {
// await pr.hide();
// await pr.show();
// }
}
static hideProgressDialog() async {
// if (pr.isShowing()) {
// await pr.hide();
// }
}
static Uint8List dataFromBase64String(String base64String) {
return base64Decode(base64String);
}
}