modulapp_utils 1.0.1
modulapp_utils: ^1.0.1 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
- dynamic check
- dynamic not
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, blue getters // Returns hexadecimal value as int
- int getValue() // Returns hexadecimal value as int
- Color 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 value
- String toHex({bool leadingHashSign = true}) // Returns hexadecimal String value
BuildContext extensions #
- extension ContextUtils on BuildContext
- MediaQueryData get mediaQuery // Shortcut to access context's MediaQuery
- Size get viewSize // Shortcut to access context's MediaQuery size
- EdgeInsets get viewInsets // Shortcut to access context's MediaQuery viewInsets
- EdgeInsets get viewPadding // Shortcut to access context's MediaQuery viewPadding
- Size get screenSize // Shortcut to access real Screen Size
final currentScreenSize = context.screenSize;
DateTime extensions #
- extension DateTimeUtils on DateTime
- DateTime parseUtc(String formattedDate) // Unformats date and returns it in the UTC time zone
- String toUtc(DateTime date) // Stringifies date and returns it in UTC time zone
- String format(String format) // Formats date
- bool get isExpired // Returns true if Now() is after date
- extension StringToDateUtils on String
- DateTime? fromFormattedDate(String format) // Tries to unformat date
- String? 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
- List
- 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 values
- MapEntry<K, V>? getWhere(bool Function(K, V) test) // Returns first MapEntry that passed test
- void removeAll(Iterable
- Map<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
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 name
- bool get isValidUrl // Returns true if (for me) it's potentially a valid Url
- bool get isEmail // Returns true if (for me) it's potentially a valid Email
- bool get isPhone // Returns true if (for me) it's potentially a valid Phone
- bool get isPostalCode // Returns true if (for me) it's potentially a valid Postal Code
- bool get isDate // Returns true if (for me) it's potentially a valid Date
- bool 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 double
- String limit([int maxLength = 100]) // Limits String's length by adding ...
- String removeDiacritics() // Removes all accented characters
- String clean() // Forces lower case and removes all accented characters
- List
- List
- String? getAllAfterFirst(String target) // Returns text placed after first target
- String? getAllAfterLast(String target) // Returns text placed after last target
- String? getAllBeforeFirst(String target) // Returns text placed before first target
- String? getAllBeforeLast(String target) // Returns text placed before last target
- String? getAllBetween(String target1, String target2) // Returns text placed between targets
- String removeLastCharacter() // Removes last character
- String removeLastZeros() // Cleans up decimals
- int get lineLength // Returns number of lines
- List
- Size 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