rluv_client/lib/global/utils.dart
2023-07-22 21:29:32 -06:00

73 lines
1.9 KiB
Dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:helpers/helpers/print.dart';
import 'package:intl/intl.dart';
String formatDate(DateTime time) {
return DateFormat('EEEE, dd MMMM yyyy').format(time);
}
DateTime dateFromJson(int value) => DateTime.fromMillisecondsSinceEpoch(value);
int? dateToJson(DateTime? value) => value?.millisecondsSinceEpoch;
bool boolFromJson(int value) => value == 1;
int boolToJson(bool hide) => hide ? 1 : 0;
String colorToJson(Color color) =>
color.toString().split('(0x')[1].split(')')[0];
Color colorFromJson(String hex) => Color(int.parse(hex, radix: 16));
String? optionalColorToJson(Color? optionalColor) => optionalColor == null
? null
: optionalColor.toString().split('(0x')[1].split(')')[0];
Color? optionalColorFromJson(String? hex) =>
hex == null ? null : Color(int.parse(hex, radix: 16));
Brightness getBrightness(Color color) =>
ThemeData.estimateBrightnessForColor(color);
List<Color> generateColorList() {
List<Color> colors = [];
for (int i = 100; i <= 255; i += 30) {
// Red value variations
for (int j = 100; j <= 255; j += 30) {
// Green value variations
for (int k = 100; k <= 255; k += 30) {
// Blue value variations
final alpha = Random().nextInt(256) + 50 % 255;
colors.add(Color.fromARGB(alpha, i, j, k));
}
}
}
colors.shuffle();
return colors;
}
extension MonetaryExtension on double {
String currency() {
final numStr = toStringAsFixed(2);
final pieces = numStr.split(".");
if (pieces.length == 1) {
return "\$${pieces.first}.00";
} else {
if (pieces.length > 2) {
printBlue(pieces);
}
return '\$${pieces[0]}.${pieces[1].padRight(2, "0")}';
}
}
}
void setDevicePortraitOrientation() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}