A comprehensive Flutter package providing useful extensions for strings, dates, context utilities, and more.
- Screen Size Utilities: Easy access to screen dimensions with scaling
- Padding Utilities: Predefined padding values for consistent spacing
- Responsive Design: Device type detection and responsive layouts
- Date Formatting: Multiple date format options
- Year Utilities: Leap year detection and year formatting
- Validation: Email, phone, URL, and numeric validation
- HTML Processing: Remove HTML tags from strings
- Phone Formatting: Convert between different phone number formats (with Turkish support)
- Null Safety: Default value handling for null/empty strings
Add this to your package's pubspec.yaml file:
dependencies:
moon_extension: ^0.0.1You can install packages from the command line:
flutter pub add moon_extensionimport 'package:moon_extension/moon_extension.dart';// Get screen dimensions
double width = context.screenWidth();
double height = context.screenHeight();
// Get scaled dimensions
double halfWidth = context.screenWidth(0.5);
double halfHeight = context.screenHeight(0.5);
// Get screen size and aspect ratio
Size screenSize = context.screenSize;
double ratio = context.aspectRatio;// Horizontal padding
Container(
padding: context.horizontal8,
child: Text('Content'),
)
// Vertical padding
Container(
padding: context.vertical16,
child: Text('Content'),
)
// All sides padding
Container(
padding: context.all16,
child: Text('Content'),
)
// Symmetric padding
Container(
padding: context.symmetric16,
child: Text('Content'),
)// Device type detection
if (context.isMobile) {
// Mobile specific code
} else if (context.isTablet) {
// Tablet specific code
} else if (context.isDesktop) {
// Desktop specific code
}
// Responsive padding
Container(
padding: context.responsivePadding,
child: Text('Responsive content'),
)final now = DateTime.now();
// Date formatting
String dateOnly = now.dateTimeFormaterWithDate(); // "25/12/2024"
String withTime = now.dateTimeFormaterWithClock(); // "25/12/2024 14:30"
String yearOnly = now.dateTimeFormaterWithYear(); // "2024"
// UTC formatting
String utcFormat = now.formatDateTimeWithoutMilliseconds(); // "2024-12-25T14:30:00Z"
// Leap year detection
int year = 2024;
bool isLeap = year.isLeapYear; // true// Email validation
String? email = "user@example.com";
bool isValidEmail = email.isValidEmail(); // true
// Phone validation
String? phone = "+90 555 123 4567";
bool isValidPhone = phone.isValidPhone(); // true
// URL validation
String? url = "https://example.com";
bool isValidUrl = url.isValidUrl(); // true
// Numeric validation
String? numeric = "12345";
bool isNumeric = numeric.isNumeric(); // true// Default value handling
String? nullString = null;
String result1 = nullString.orDefault(); // "-"
String? emptyString = "";
String result2 = emptyString.orDefault(); // "-"
String? customDefault = nullString.orDefaultWith("No Data"); // "No Data"
// Null/empty checks
bool isNullOrEmpty = nullString.isNullOrEmpty; // true
bool isNotNullOrEmpty = "Hello".isNotNullOrEmpty; // true// Remove HTML tags
String? htmlText = "<p>Hello <strong>World</strong>!</p>";
String cleanText = htmlText.removeHtmlTags(); // "Hello World!"// Convert to international format
String? phone1 = "(555) 123 4567";
String international = phone1.toInternationalFormat(); // "+905551234567"
String? phone2 = "05551234567";
String international2 = phone2.toInternationalFormat(); // "+905551234567"
// Convert to simple format
String? phone3 = "+905551234567";
String simple = phone3.toSimpleFormat(); // "5551234567"Check out the example directory for a complete demonstration of all features.
To run the example:
cd example
flutter runThis package depends on:
intl: ^0.19.0- For date formatting and internationalization
This package supports all Flutter platforms:
- β Android
- β iOS
- β Web
- β Windows
- β macOS
- β Linux
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG.md for a list of changes and version history.