Files
Neo_wallet/neowallet_mobile/lib/utils/utils.dart
Manasit.K 43c32ef6cf init
2024-10-31 15:57:57 +07:00

356 lines
9.2 KiB
Dart

import 'dart:developer';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
void printApiStatus(http.Response response) {
log("header : " + response.request!.headers.toString());
log(response.request!.url.toString());
log(response.statusCode.toString());
log(response.body.toString());
}
void printLongString(String text) {
final RegExp pattern = RegExp('.{1,800}'); // 800 is the size of each chunk
pattern.allMatches(text).forEach((RegExpMatch match) => print(match.group(0)));
}
class Utils {
static String convertDateFromMilli(String? selectedDate) {
if (selectedDate == null) {
return "";
}
var date = DateFormat('yyyy-MM-ddTHH:mm:ss').parse(selectedDate);
return DateFormat('d MMM yyyy').format(date);
}
static String convertDateToBase(String? selectedDate) {
if (selectedDate == null || selectedDate.isEmpty) {
return "";
}
// print(selectedDate);
// var date =
// DateFormat('yyyy-MM-dd HH:mm:ss').parseUTC(selectedDate).toLocal();
// return DateFormat('d MMM yy HH:mm').format(date);
try {
var date = DateFormat('yyyy-MM-dd HH:mm:ss').parseUTC(selectedDate).toLocal();
return DateFormat('d MMM yy HH:mm').format(date);
} catch (e) {
return selectedDate;
}
}
static String convertDateToDay(String? selectedDate,BuildContext context) {
if (selectedDate == null || selectedDate.isEmpty) {
return "";
}
// print(selectedDate);
// var date =
// DateFormat('yyyy-MM-dd HH:mm:ss').parseUTC(selectedDate).toLocal();
// return DateFormat('d MMM yy HH:mm').format(date);2023-08-16T06:04:02.74949+00:00
try {
var date = DateFormat('yyyy-MM-ddTHH:mm:ss').parse(selectedDate);
return DateFormat('d MMMM yyyy',context.locale.toString()).format(date);
} catch (e) {
return selectedDate;
}
}
static String convertDateToTime(String? selectedDate) {
if (selectedDate == null || selectedDate.isEmpty) {
return "";
}
// print(selectedDate);
// var date =
// DateFormat('yyyy-MM-dd HH:mm:ss').parseUTC(selectedDate).toLocal();
// return DateFormat('d MMM yy HH:mm').format(date);2023-08-16T06:04:02.74949+00:00
try {
var date = DateFormat('yyyy-MM-ddTHH:mm:ss').parse(selectedDate);
return DateFormat('HH:mm').format(date);
} catch (e) {
return selectedDate;
}
}
static String convertDateToBaseReal(String? selectedDate) {
if (selectedDate == null || selectedDate.isEmpty) {
return "";
}
try {
var date = DateFormat('yyyy-MM-dd HH:mm:ss').parse(selectedDate);
return DateFormat('d MMM yy HH:mm').format(date);
} catch (e) {
return selectedDate;
}
}
static String convertDatePlayback(String? selectedDate) {
if (selectedDate == null || selectedDate.isEmpty) {
return "";
}
var date = DateFormat('yyyy-MM-dd HH:mm:ss').parse(selectedDate);
return DateFormat('d MMM yy HH:mm:ss').format(date);
}
static String convertDate(DateTime? selectedDate) {
if (selectedDate == null) {
return "";
}
return DateFormat('dd/MM/yyyy').format(selectedDate);
}
static String convertDateTH(DateTime? selectedDate) {
if (selectedDate == null) {
return "";
}
return DateFormat('dd/MM/yyyy', 'th').format(selectedDate);
}
static String convertDateToPost(DateTime? selectedDate,String lang) {
if (selectedDate == null) {
return "";
}
return DateFormat('d MMM yyyy',lang).format(selectedDate);
}
static String convertDatePickup(String? selectedDate) {
if (selectedDate == null) {
return "";
}
var date = DateFormat('yyyy-MM-ddTHH:mm:ss').parse(selectedDate);
return DateFormat('dd/MM/yyyy').format(date);
}
static DateTime convertDateStringToDateTime(String? selectedDate) {
if (selectedDate == null) {
return DateTime.now();
}
var date = DateFormat('yyyy-MM-ddTHH:mm:ss.SSS').parse(selectedDate);
return date;
}
static String getDateGraph(String? selectedDate) {
if (selectedDate == null) {
return "";
}
try {
var date = DateFormat('yyyy-MM-dd').parse(selectedDate);
return DateFormat('d MMM').format(date) + "\n" + DateFormat('yyyy').format(date);
} catch (e) {
return selectedDate;
}
}
// 2021-09-28T07:27:37.873Z
static getDateInitPromptPay() {
return DateFormat('yyyyddHH').format(new DateTime.now());
}
static getDateCreate() {
return DateFormat('yyyy-MM-dd').format(new DateTime.now());
}
static getDateTimeCreate() {
return DateFormat('d-MM-yyyy HH:mm').format(new DateTime.now());
}
static getTimestamp() {
return DateFormat('yyyyMMddHHmmss').format(new DateTime.now());
}
static getResponseTime() {
return DateFormat('yyyy-MM-ddTHH:mm:ss.SSSZ').format(new DateTime.now());
}
static getDateBackYear() {
var d = DateTime.now().subtract(Duration(days: 30));
return DateFormat('yyyy-MM-dd').format(d);
}
static getDateDisplay(String lang) {
return DateFormat('d MMM yyyy',lang).format(new DateTime.now());
}
static getDatePickup(String selectedDate) {
var date = DateFormat('dd/MM/yyyy').parse(selectedDate);
return DateFormat('yyyy-MM-dd').format(date);
}
static dateFromServerToPost(String selectedDate) {
var date = DateFormat('yyyy-MM-ddTHH:mm:ss').parse(selectedDate);
return DateFormat('yyyy-MM-dd').format(date);
}
static moneyFormat(var amount) {
if (amount == null || amount.toString().isEmpty) {
return "0";
}
var formatter = NumberFormat('#,###,###.##');
if (amount is int) {
return formatter.format(amount);
} else if (amount is double) {
return formatter.format(amount);
} else {
return formatter.format(double.tryParse(amount));
}
}
static Widget getRisk(int? risk_id) {
if (risk_id == null) {
return Container();
}
if (risk_id == 0) {
return Icon(
Icons.circle,
color: Colors.red,
size: 40,
);
} else if (risk_id == 1) {
return Icon(
Icons.circle,
color: Colors.yellow,
size: 40,
);
} else {
return Icon(
Icons.circle,
color: Colors.green,
size: 40,
);
}
}
static showAlertDialog(BuildContext context, String message) {
// set up the button
Widget okButton = ElevatedButton(
child: Text("OK"),
onPressed: () {
Navigator.pop(context);
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
content: Text(message),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
static showAlertDialogCallback(BuildContext context, String message, ValueChanged action) {
// set up the button
Widget okButton = ElevatedButton(
child: Text("OK"),
onPressed: () {
Navigator.pop(context);
action.call("");
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
content: Text(message),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return alert;
},
);
}
static String numberFormat(double number) {
var formatter = NumberFormat('#,###,##0.0');
try {
return formatter.format(number);
} catch (e) {
return number.toString();
}
}
static String numberFormatInt(int number) {
var formatter = NumberFormat('#,###,##0');
try {
return formatter.format(number);
} catch (e) {
return number.toString();
}
}
static double checkDouble(dynamic value) {
if (value is String) {
return double.parse(value);
} else {
return value.toDouble();
}
}
static showAlertDialogEmpty(BuildContext context) {
// set up the button
Widget okButton = ElevatedButton(
child: Text("Go back"),
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("No data found"),
content: Text("Please Try Again"),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
static bool isLoad = false;
static void loadingProgress(BuildContext context) {
isLoad = true;
AlertDialog alert = AlertDialog(
content: new Row(
children: [
CircularProgressIndicator(),
Container(margin: EdgeInsets.only(left: 5), child: Text("Loading..")),
],
),
);
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
static bool emailValidate(String email) {
return RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(email);
}
}