customcommonfunctions 0.0.7
The commonfunctions package provides utility functions for simplifying date, time, and string operations in Flutter apps, including custom date and time pickers, formatting, color conversion, string manipulations, email validation, and utilities for managing monthly data.
commonfunctions
A Flutter package that provides a collection of common utility functions for handling date pickers, time pickers, string manipulations, color conversions,first and last dates of month and more.
Features
- Custom Date Picker: A user-friendly date selection widget with options for both past and future dates.
- Custom Time Picker: A customizable time selection dialog that formats time in both 12-hour and 24-hour formats.
- Color Conversion: Convert string-based color codes (e.g., #FF5733) to Flutter Color objects.
- Date and Time Formatting: Easily format and retrieve the current date and time in various styles using the intl package.
- String Manipulations: Functions for calculating string length and converting strings to Base64.
- Email Validation: Validate email addresses using regex patterns.
- First and Last Dates of Month: Retrieve the first and last dates of a given month, making it easy to manage monthly data.
Implementation Details
-
Custom Date Picker The CustomDatePicker widget allows you to present a date picker dialog to the user. You can specify an initial date, as well as the range of dates that the user can select from (e.g., past or future dates).
-
CustomDatePicker( context: context, initialDate: DateTime.now(), // Initial date displayed in the picker firstDate: DateTime(2000), // Earliest date that can be selected lastDate: DateTime(2100), // Latest date that can be selected onDateSelected: (selectedDate) { print('Selected date: $selectedDate'); }, );
Make sure to pass the context parameter. The onDateSelected callback will return the selected DateTime object
- Custom Time Picker
The CustomTimePicker provides a time selection dialog with the ability to set the initial time
- CustomTimePicker( context: context, initialTime: TimeOfDay.now(), // Default time shown when the picker opens onTimeSelected: (selectedTime) { print('Selected time: $selectedTime'); }, );
The onTimeSelected callback provides the selected TimeOfDay object
- Convert String to Color
The ColorUtils class helps you convert a hexadecimal color string (e.g., #FF5733) into a Flutter Color object.
Color color = ColorUtils.stringToColor("#FF5733");
This method is helpful for applying dynamic colors retrieved from a backend or configuration.
- Get the Current Date and Time in a Formatted String
You can retrieve and format the current date and time using DateUtils. The formatting follows the intl package's patterns.
String formattedDate = DateUtils.formatDate(DateTime.now(), 'dd-MM-yyyy'); print(formattedDate); // Example output: 27-09-2024
Change the date format string ('dd-MM-yyyy') to suit your requirements
- Convert Strings to Base64
The StringManipulation class provides a way to convert strings into their Base64-encoded representations.
String originalText = "Hello, Flutter!"; String base64Text = StringManipulation.toBase64(originalText); print(base64Text); // Output: SGVsbG8sIEZsdXR0ZXIh
- Format Timestamps into 12-hour AM/PM Format
You can convert a DateTime object to a 12-hour AM/PM format string using DateUtils.
String formattedTime = DateUtils.formatTo12Hour(DateTime.now()); print(formattedTime); // Output: 02:30 PM (example)
- Calculate the Length of a String
The StringManipulation class lets you calculate the length of a string.
int length = StringManipulation.calculateLength("Flutter"); print(length); // Output: 7
- Validate Email Addresses
The EmailValidator class provides a way to validate email addresses using regex.
bool isValidEmail = EmailValidator.validate("example@domain.com"); print(isValidEmail); // Output: true
Use this function to quickly validate email input in forms.
- Get the First and Last Dates of Any Given Month
You can retrieve the first and last dates of any specified month using DateUtils.
DateTime selectedMonth = DateTime(2024, 9); // September 2024 DateTime firstDate = DateUtils.getFirstDateOfMonth(selectedMonth); DateTime lastDate = DateUtils.getLastDateOfMonth(selectedMonth);
print('First Date: $firstDate'); // Output: First Date: 2024-09-01 00:00:00.000 print('Last Date: $lastDate'); // Output: Last Date: 2024-09-30 00:00:00.000
This is useful for managing data on a monthly basis.
Example
Here's a comprehensive example that demonstrates the implementation of multiple features from the package:
// Import the commonfunctions package
import 'package:commonfunctions/commonfunctions.dart';
void main() {
// Example usage of the Custom Date Picker
CustomDatePicker(
context: context, // Replace 'context' with your BuildContext
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2100),
onDateSelected: (selectedDate) {
print('Selected date: $selectedDate');
},
);
// Example usage of the Custom Time Picker
CustomTimePicker(
context: context, // Replace 'context' with your BuildContext
initialTime: TimeOfDay.now(),
onTimeSelected: (selectedTime) {
print('Selected time: $selectedTime');
},
);
// Convert a string to a color object
Color color = ColorUtils.stringToColor("#FF5733");
print('Converted Color: $color');
// Formatting the current date
String formattedDate = DateUtils.formatDate(DateTime.now(), 'dd-MM-yyyy');
print('Formatted Date: $formattedDate');
// Converting a string to Base64
String base64Text = StringManipulation.toBase64("Hello, Flutter!");
print('Base64 Encoded: $base64Text');
// Validating an email address
bool isValidEmail = EmailValidator.validate("example@domain.com");
print('Is valid email: $isValidEmail');
// Getting the first and last dates of a month
DateTime selectedMonth = DateTime(2024, 9); // Example: September 2024
DateTime firstDate = DateUtils.getFirstDateOfMonth(selectedMonth);
DateTime lastDate = DateUtils.getLastDateOfMonth(selectedMonth);
print('First Date: $firstDate, Last Date: $lastDate');
}
## Installation
Add this line to your `pubspec.yaml` file under dependencies:
```yaml
dependencies:
commonfunctions: ^0.0.1