modulapp_utils 1.0.3
modulapp_utils: ^1.0.3 copied to clipboard
A set of useful (or not) methods and type extensions
Modulapp Utils #
A set of static methods/getters to core dependency injection, logging, etc, across app's modules/sfeatures
Animation mixin #
This mixin simplify the implementation of an animated widget
Use that mixin on a StatefulWidget's state like this:
class MyClassState extends State<MyClass> with SingleTickerProviderStateMixin, Animated<MyClass>
bool extensions #
- extension BooleanUtils on bool
dynamic let<T>(T Function() map, {...})// For lazy devs which want to do "one line" computations on booldynamic check<T>(T result, {dynamic orIfFalse})// Returns something depending if bool is truedynamic not<T>(T result, {dynamic orIfTrue})// Returns something depending if bool is false
Here is .check() example:
class SimpleButton extends StatelessWidget {
final VoidCallback? onTap;
final String text;
final bool highlight;
const SimpleButton({
super.key,
required this.text,
this.onTap,
this.highlight = false,
});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onTap,
// NOTE: here if [hightlight] is true then background will be red
style: ButtonStyle(backgroundColor: highlight.check(Colors.red)),
child: Text(text),
);
}
}
Color extensions #
- extension ColorUtils on Color
alpha, red, green, bluegetters // Returns hexadecimal value as intint getValue()// Returns hexadecimal value as intColor darken([int percent = 10])// Darken a color by [percent] amount (100 = black)Color lighten([int percent = 10])// Lighten a color by [percent] amount (100 = white)Color fromHex(String hexString)// Returns color from hexadecimal String valueString toHex({bool leadingHashSign = true})// Returns hexadecimal String value
BuildContext extensions #
- extension ContextUtils on BuildContext
MediaQueryData get mediaQuery// Shortcut to access context's MediaQuerySize get viewSize// Shortcut to access context's MediaQuery sizeEdgeInsets get viewInsets// Shortcut to access context's MediaQuery viewInsetsEdgeInsets get viewPadding// Shortcut to access context's MediaQuery viewPaddingSize get screenSize// Shortcut to access real Screen Size- e.g.
final currentScreenSize = context.screenSize;
- e.g.
DateTime extensions #
- extension DateTimeUtils on DateTime
DateTime parseUtc(String formattedDate)// Unformats date and returns it in the UTC time zoneString toUtc(DateTime date)// Stringifies date and returns it in UTC time zoneString format(String format)// Formats datebool get isExpired// Returns true if Now() is after date
- extension StringToDateUtils on String
DateTime? fromFormattedDate(String format)// Tries to unformat dateString? reformat(String fromFormat, String toFormat)// Unformats then reformats date
- extension NullableStringToDateUtils on String?
DateTime? get asDate// Tries to parse String as date
Iterable extensions #
- extension ListUtils
- extension IterableUtils
- extension NestedIterableUtils
- extension IterableNullableUtils
- extension PairListUtils<T, V> on Iterable<Pair<T, V>>
List<T> get firsts// Returns all Pairs' first valueList<V> get seconds// Returns all Pairs' second value
- extension MapEntryListUtils<T, V> on Iterable<MapEntry<T, V>>
Map<T, V> toMap()// Returns a Map from a MapEntry list
- extension ToListUtils on Object?
List toList()// Wraps an Object in a List
- extension MapUtils<K, V> on Map<K, V>
Map<K, V> clone()// Returns a new Map with same valuesMapEntry<K, V>? getWhere(bool Function(K, V)test) // Returns first MapEntry that passed testvoid removeAll(Iterable<K> keys)// Removes items with key that matchMap<K, V> sort(int Function(MapEntry<K, V>, MapEntry<K, V>) sortFct)// Sorts Map depending on sortFct
Object extensions #
- extension ObjectNullableUtils on Object?
dynamic let<T>(T Function() map, {...})// For lazy devs which want to do "one line" computations on nullable Objects
Here is .let() example:
class SimpleItem extends StatelessWidget {
final String? title;
final String? subtitle;
final String? description;
const SimpleItem({super.key, this.title, this.subtitle, this.description});
@override
Widget build(BuildContext context) {
return Column(
children: [
title.let((e) => Text(e), orIfNull: () => const Text('Title is missing')),
?subtitle.let((e) => Text(e)),
?description.let((e) => Text(e)),
],
);
}
}
Regex extensions #
- extension RegexUtils on String
bool get isValidName// Returns true if (for me) it's potentially a valid namebool get isValidUrl// Returns true if (for me) it's potentially a valid Urlbool get isSocialNumber// Returns true if (for me) it's potentially a valid social numberbool get isSIRET// Returns true if (for me) it's potentially a valid SIRETbool get isEmail// Returns true if (for me) it's potentially a valid Emailbool get isPhone// Returns true if (for me) it's potentially a valid Phonebool get isPostalCode// Returns true if (for me) it's potentially a valid Postal Codebool get isDate// Returns true if (for me) it's potentially a valid Datebool get isValidFileName// Returns true if (for me) it's potentially a valid File Name
String extensions #
- extension StringUtils on String
double? parseAsDouble()// Tries to parse String as doubleString limit([int maxLength = 100])// Limits String's length by adding ...String removeDiacritics()// Removes all accented charactersString clean()// Forces lower case and removes all accented charactersList<String> splitWords()// Splits all words separated by non alphabetic charactersList<String> cleanAndSplit()// Forces lower case and removes all accents and splits all wordsString? getAllAfterFirst(String target)// Returns text placed after first targetString? getAllAfterLast(String target)// Returns text placed after last targetString? getAllBeforeFirst(String target)// Returns text placed before first targetString? getAllBeforeLast(String target)// Returns text placed before last targetString? getAllBetween(String target1, String target2)// Returns text placed between targetsString removeLastCharacter()// Removes last characterString removeLastZeros()// Cleans up decimalsint get lineLength// Returns number of linesList<String> searchWords(List<String> searchedWords)// Search for words ignoring case and accentsSize computeSize(double fontSize, String fontFamily, {FontWeight? fontWeight, FontStyle? fontStyle})// Returns rendered text size with small security margins
Widget extensions #
- extension WidgetUtils on Widget
Widget wrapWithHeroIfTagNotNull(String? tag)// Wraps with a Hero widget if tag not null
- extension WidgeStatePropertytUtils