taafee-mobile/lib/core/utils/utils.dart
2023-10-17 17:22:55 +03:00

110 lines
3.0 KiB
Dart

import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
class Utils {
// static String? requestTypeToString(RequestMethod requestType) {
// String? type = '';
// switch (requestType) {
// case RequestMethod.get:
// {
// type = 'GET';
// }
// break;
// case RequestMethod.post:
// {
// type = 'POST';
// }
// break;
// case RequestMethod.delete:
// {
// type = 'DELETE';
// }
// break;
// case RequestMethod.patch:
// {
// type = 'PATCH';
// }
// break;
// case RequestMethod.put:
// {
// type = 'PUT';
// }
// break;
// }
// return type;
// }
static String formatDateDifference(DateTime date) {
final now = DateTime.now();
final difference = now.difference(date).inDays;
if (difference == 0) {
return 'today'.tr;
} else if (difference == 1) {
return 'yesterday'.tr;
} else if (difference >= 2 && difference <= 6) {
final weekdayFormat = DateFormat.EEEE(); // Format to get the weekday name
return weekdayFormat.format(date).tr;
} else if (difference >= 7 && difference <= 13) {
return 'last_week'.tr;
} else if (difference >= 14 && difference <= 20) {
return '2_weeks_ago'.tr;
} else {
return 'long_time_ago'.tr;
}
}
static Future<List<AssetEntity>?> pickImage(BuildContext context) async {
return await AssetPicker.pickAssets(
context,
pickerConfig: const AssetPickerConfig(maxAssets: 50),
);
}
static Future<File?> pickSingleImage(BuildContext context) async {
List<AssetEntity>? pickedAssets = await AssetPicker.pickAssets(
context,
pickerConfig: const AssetPickerConfig(maxAssets: 1),
);
if (pickedAssets != null) {
AssetEntity assetEntity = pickedAssets[0];
return await assetEntity.file;
}
return null;
}
static String randomString([int length = 10]) {
var random = Random();
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
return String.fromCharCodes(Iterable.generate(
length, (_) => chars.codeUnitAt(random.nextInt(chars.length))));
}
static MaterialColor createMaterialColor(Color color) {
List<double> strengths = <double>[.05];
Map<int, Color> swatch = <int, Color>{};
final int r = color.red, g = color.green, b = color.blue;
for (int i = 1; i < 10; i++) {
strengths.add(0.1 * i);
}
for (final double strength in strengths) {
final double ds = 0.5 - strength;
swatch[(strength * 1000).round()] = Color.fromRGBO(
r + ((ds < 0 ? r : (255 - r)) * ds).round(),
g + ((ds < 0 ? g : (255 - g)) * ds).round(),
b + ((ds < 0 ? b : (255 - b)) * ds).round(),
1,
);
}
return MaterialColor(color.value, swatch);
}
}