This commit is contained in:
unknown
2021-08-30 22:38:58 +02:00
parent 9544d89993
commit 46fa86f989
152 changed files with 4074 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
class ColorUtils {
static Color stringToColor(String str) {
int hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.codeUnitAt(i) + ((hash << 5) - hash);
}
return HSLColor.fromAHSL(1, hash % 360, .8, .75).toColor();
}
static Color foregroundColor(Color color) => color.computeLuminance() >= .5 ? Colors.black : Colors.white;
}

View File

@@ -0,0 +1,55 @@
import 'package:flutter/widgets.dart';
import 'package:intl/intl.dart';
import 'package:i18n_extension/i18n_widget.dart';
import 'package:html/parser.dart';
extension StringFormatUtils on String {
String specialChars() => this
.replaceAll("é", "e")
.replaceAll("á", "a")
.replaceAll("ó", "o")
.replaceAll("ő", "o")
.replaceAll("ö", "o")
.replaceAll("ú", "u")
.replaceAll("ű", "u")
.replaceAll("ü", "u")
.replaceAll("í", "i");
String capital() => this.length > 0 ? this[0].toUpperCase() + this.substring(1) : "";
String capitalize() => this.split(" ").map((w) => this.capital()).join(" ");
String escapeHtml() {
String htmlString = this;
htmlString = htmlString.replaceAll("\r", "");
htmlString = htmlString.replaceAll(RegExp(r'<br ?/?>'), "\n");
htmlString = htmlString.replaceAll(RegExp(r'<p ?>'), "");
htmlString = htmlString.replaceAll(RegExp(r'</p ?>'), "\n");
var document = parse(htmlString);
return document.body?.text.trim() ?? "";
}
}
extension DateFormatUtils on DateTime {
String format(BuildContext context, {bool timeOnly = false, bool weekday = false}) {
// Time only
if (timeOnly) return DateFormat("HH:mm").format(this);
DateTime now = DateTime.now();
if (this.difference(now).inDays == 0) {
if (this.hour == 0 && this.minute == 0 && this.second == 0) return "Today"; // TODO: i18n
return DateFormat("HH:mm").format(this);
}
if (this.difference(now).inDays == 1) return "Yesterday"; // TODO: i18n
String formatString;
if (this.year == now.year)
formatString = "MMM dd.";
else
formatString = "yy/MM/dd";
if (weekday) formatString += " (EEEE)";
return DateFormat(formatString, I18n.of(context).locale.toString()).format(this);
}
}

View File

@@ -0,0 +1,18 @@
import 'dart:convert';
class JwtUtils {
static String? getNameFromJWT(String jwt) {
var parts = jwt.split(".");
if (parts.length != 3) return null;
if (parts[1].length % 4 == 2) {
parts[1] += "==";
} else if (parts[1].length % 4 == 3) {
parts[1] += "=";
}
var payload = utf8.decode(base64Url.decode(parts[1]));
var jwtData = jsonDecode(payload);
return jwtData["name"];
}
}