rluv_client/lib/global/utils.dart

124 lines
3.6 KiB
Dart
Raw Permalink Normal View History

2023-07-22 21:29:32 -06:00
import 'dart:math';
2023-07-19 02:16:13 -06:00
import 'package:colorful_print/colorful_print.dart';
2023-07-22 21:29:32 -06:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2023-07-27 01:40:26 -06:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-07-19 02:16:13 -06:00
import 'package:intl/intl.dart';
2023-07-27 01:40:26 -06:00
import 'package:rluv/global/styles.dart';
import 'package:rluv/main.dart';
2023-07-19 02:16:13 -06:00
String formatDate(DateTime time) {
return DateFormat('EEEE, dd MMMM yyyy').format(time);
}
DateTime dateFromJson(int value) => DateTime.fromMillisecondsSinceEpoch(value);
2023-07-22 21:29:32 -06:00
int? dateToJson(DateTime? value) => value?.millisecondsSinceEpoch;
bool boolFromJson(int value) => value == 1;
int boolToJson(bool hide) => hide ? 1 : 0;
2023-07-19 02:16:13 -06:00
String colorToJson(Color color) => color.toString().split('(0x')[1].split(')')[0];
2023-07-19 02:16:13 -06:00
Color colorFromJson(String hex) => Color(int.parse(hex, radix: 16));
2023-07-22 21:29:32 -06:00
String? optionalColorToJson(Color? optionalColor) =>
optionalColor == null ? null : optionalColor.toString().split('(0x')[1].split(')')[0];
2023-07-22 21:29:32 -06:00
Color? optionalColorFromJson(String? hex) => hex == null ? null : Color(int.parse(hex, radix: 16));
2023-07-22 21:29:32 -06:00
Brightness getBrightness(Color color) => ThemeData.estimateBrightnessForColor(color);
2023-07-22 21:29:32 -06:00
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) {
printColor(pieces, textColor: TextColor.blue);
2023-07-22 21:29:32 -06:00
}
return '\$${pieces[0]}.${pieces[1].padRight(2, "0")}';
}
}
}
void setDevicePortraitOrientation() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
2023-07-27 01:40:26 -06:00
enum SnackType { info, success, error }
void showSnack(
{required WidgetRef ref,
required String text,
SnackType type = SnackType.info,
Duration duration = const Duration(seconds: 2)}) {
final messengerKey = ref.read(scaffoldMessengerKeyProvider);
if (messengerKey.currentState == null) {
printColor('Cannot show snackbar, state == null', textColor: TextColor.red);
2023-07-27 01:40:26 -06:00
return;
}
final color = type == SnackType.info
? Styles.washedStone
: type == SnackType.success
? Styles.seaweedGreen
: Styles.expensesRed;
final textStyle = TextStyle(
fontSize: 16,
color: color,
);
messengerKey.currentState!.showSnackBar(SnackBar(
elevation: 8,
backgroundColor: Styles.deepPurpleNurple,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(topLeft: Radius.circular(20.0), topRight: Radius.circular(20.0)),
2023-07-27 01:40:26 -06:00
),
content: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 14.0),
child: Icon(type == SnackType.success ? Icons.check_circle : Icons.info, color: color),
2023-07-27 01:40:26 -06:00
),
Text(text, style: textStyle),
],
),
),
));
}
bool isEmailValid(String email) {
final RegExp regex = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$");
2023-07-27 01:40:26 -06:00
return regex.hasMatch(email);
}
bool isUsernameValid(String username) {
final RegExp regex = RegExp(r"[a-zA-Z0-9._-]");
return regex.hasMatch(username);
}