Utility Library
A comprehensive set of Dart extensions and utilities to enhance your Flutter applications with additional functionality like advanced number, date, and string manipulations.
Features
- Color utilities
- Extended number formatting and validation
- Date and time formatting and computations
- String transformations and checks
- Unique and JSON transformation functionalities for collections
Installation
To use the utility library in your project, add it to your dependencies in your pubspec.yaml
file:
dependencies:
utility_library: ^1.0.0
Then run:
flutter pub get
Usage
Import the library
Add the following import to your Dart file:
import 'package:utility_library/utility_library.dart';
Color Utilities
Set your app's primary and secondary colors:
AppColors.primaryColor = Color(0xff02a4ff); // Your app primary color
AppColors.secondaryColor = Color(0xffffffff); // Your app secondary color
JSON Extensions
You can use an interface or abstract class with instance methods for toJson and factory or regular methods for fromJson. Here’s a revised approach:
User user = User(name: 'Alice', age: 28);
String userJson = serialize(user);
print('Serialized User: $userJson');
User deserializedUser = deserialize<User>(userJson, User.fromJson);
print('Deserialized User: ${deserializedUser.name}');
Number Extensions
Formatting and checking numbers:
double myNumber = 123.456;
print(myNumber.reFormat(formatDigits: '000', asFixedNumber: 3)); // Using the extension method
DateTime Extensions
Work with DateTime formatting easily:
DateTime now = DateTime.now();
print('Standard DateTime: ${now.format(pattern: PredefinedDateFormats.standardDateTime.pattern)}');
print('Date Only: ${now.format(pattern: PredefinedDateFormats.dateOnly.pattern)}');
print('Time Only: ${now.format(pattern: PredefinedDateFormats.timeOnly.pattern)}');
print('Custom DateTime: ${now.format(pattern: PredefinedDateFormats.customDateTime.pattern)}');
print('Verbose DateTime with Day of Week: ${now.format(pattern: PredefinedDateFormats.verboseDateTimeWithDayOfWeek.pattern)}');
print('Year and Full Month: ${now.format(pattern: PredefinedDateFormats.yearAndFullMonth.pattern)}');
Date Time Utils Extensions
Work with DateTime formatting easily:
DateTime today = DateTime.now();
print('Today is ${today.isLeapYear ? "a" : "not a"} leap year.');
print('Days in the current month: ${today.daysInMonth}');
print('Is today the start of the year? ${today.isStartOfYear}');
print('Is today the end of the year? ${today.isEndOfYear}');
print('Days to the end of the year: ${today.daysToEndOfYear}');
print('Is today a weekend? ${today.isWeekend}');
More Utilities
- Checking leap year, day computations, weekend checks for dates.
- Validating and formatting for doubles and integers.
- String manipulations such as title casing, palindrome checks, and base64 conversions.
- Unique and JSON transformations for lists.
Example
Here's how you might use some of these utilities in a Flutter app:
// Example usage of the library
void main() {
// Initialize and use colors
AppColors.primaryColor = Color(0xff02a4ff);
AppColors.secondaryColor = Color(0xffffffff);
// Work with extensions
DateTime today = DateTime.now();
print('Today is ${today.isLeapYear ? "a" : "not a"} leap year.');
}
// Example usage of the library
Text(widget.title, style: TextStyle(color: AppColors.secondaryColor))
pubspec.yaml
dependencies:
flutter:
sdk: flutter
intl: ^0.17.0
Contributing
Contributions are welcome! Feel free to submit pull requests or create issues for bugs and feature requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
words unique? ${words.isUnique}'); print('JSON String of words: ${words.toJsonString()}'); print('Transformed numbers: ${numbers.transform((n) => 'Number $n').toString()}');
## Contributing
Contributions are welcome! Feel free to submit pull requests or create issues for bugs and feature requests.
## License
This project is licensed under the MIT License - see the LICENSE file for details.