73 lines
2.2 KiB
Dart
73 lines
2.2 KiB
Dart
class Helpers {
|
|
static String formatCurrency(double amount) {
|
|
return 'Rp ${amount.toStringAsFixed(0).replaceAllMapped(
|
|
RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'),
|
|
(Match m) => '${m[1]}.',
|
|
)}';
|
|
}
|
|
|
|
static String formatDate(DateTime date) {
|
|
final months = [
|
|
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
|
|
];
|
|
return '${date.day} ${months[date.month - 1]} ${date.year}, ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
|
|
}
|
|
|
|
static String formatDateOnly(DateTime date) {
|
|
final months = [
|
|
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
|
|
];
|
|
return '${date.day} ${months[date.month - 1]} ${date.year}';
|
|
}
|
|
|
|
static String formatTime(DateTime date) {
|
|
return '${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
|
|
}
|
|
|
|
static String getTimeAgo(DateTime date) {
|
|
final now = DateTime.now();
|
|
final difference = now.difference(date);
|
|
|
|
if (difference.inDays > 0) {
|
|
return '${difference.inDays} hari yang lalu';
|
|
} else if (difference.inHours > 0) {
|
|
return '${difference.inHours} jam yang lalu';
|
|
} else if (difference.inMinutes > 0) {
|
|
return '${difference.inMinutes} menit yang lalu';
|
|
} else {
|
|
return 'Baru saja';
|
|
}
|
|
}
|
|
|
|
static String capitalize(String text) {
|
|
if (text.isEmpty) return text;
|
|
return text[0].toUpperCase() + text.substring(1).toLowerCase();
|
|
}
|
|
|
|
static String truncateText(String text, int maxLength) {
|
|
if (text.length <= maxLength) return text;
|
|
return '${text.substring(0, maxLength)}...';
|
|
}
|
|
|
|
static bool isValidEmail(String email) {
|
|
return RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(email);
|
|
}
|
|
|
|
static bool isValidPhone(String phone) {
|
|
return RegExp(r'^\+?[\d\s-]+$').hasMatch(phone);
|
|
}
|
|
|
|
static String generateTrackingNumber() {
|
|
final now = DateTime.now();
|
|
final timestamp = now.millisecondsSinceEpoch.toString();
|
|
return 'TRK${timestamp.substring(timestamp.length - 6)}';
|
|
}
|
|
|
|
static String generateOrderId() {
|
|
final now = DateTime.now();
|
|
final timestamp = now.millisecondsSinceEpoch.toString();
|
|
return 'ORD${timestamp.substring(timestamp.length - 8)}';
|
|
}
|
|
} |