magic_extensions
magic_extensions
is a Dart and Flutter package that provides a collection of extension methods for common Dart and Flutter types, enhancing their functionality and usability. This library simplifies common operations and adds convenience methods to core types.
Features
- DateTime Extensions: Utility functions for formatting and comparing dates.
- Integer Extensions: Validation for status codes.
- String Extensions: Utilities for string manipulation and conversion.
- TimeOfDay Extensions: Methods for comparing and formatting time values.
- Status Colors: A centralized way to manage and use status-related colors.
Installation
To use magic_extensions in your Flutter project, add the following dependency to your pubspec.yaml file:
dependencies:
magic_extensions: ^latest_version
This Documentation Is Outdated and Does Not Contains Explanation Of All the methods and extensions please refer to API reference
Usage
DateTime Extensions
import 'package:magic_extensions/magic_extensions.dart';
DateTime? date = DateTime.now();
String formattedDate = date.format(format: 'yyyy-MM-dd');
bool isToday = date.isToday;
Integer Extensions
import 'package:magic_extensions/magic_extensions.dart';
int? statusCode = 200;
bool isValid = statusCode.validateStatusCode(startStatusCode: 200, endStatusCode: 299);
String Extensions
import 'package:magic_extensions/magic_extensions.dart';
String? text = "Hello, World!";
String withPrefix = text.withPrefix(prefix: 'Prefix: ');
String withSuffix = text.withSuffix(suffix: ' Suffix');
DateTime? dateTime = text.toDateTime;
String formattedTime = text.toTimeString(format: 'hh:mm a');
TimeOfDay Extensions
import 'package:magic_extensions/magic_extensions.dart';
TimeOfDay? time = TimeOfDay.now();
bool isLater = time.isAfter(TimeOfDay(hour: 9, minute: 0));
String formattedTime = time.format(format: 'hh:mm a');
Status Colors
import 'package:magic_extensions/magic_extensions.dart';
import 'package:flutter/material.dart';
Map<String, Color> colorMap = {
'success': Colors.green,
'error': Colors.red,
'default': Colors.grey,
};
Color color = 'success'.toMyStatusColor(colorMap);
API
MagicDateExtension
- String format({String format = 'dd MMM, yyyy', String placeholder = ''})
- bool get isToday
- bool isBeforeWithoutTime(DateTime? otherDate)
- bool isAfterWithoutTime(DateTime? otherDate)
MagicIntExtension
- bool validateStatusCode({int startStatusCode = 200, int endStatusCode = 299})
- MagicNullString
- bool get isNotNullAndNotEmpty
- String get withComma
- String withPrefix({required String prefix, String placeholder = ''})
- String withSuffix({required String suffix, String placeholder = ''})
- DateTime? get toDateTime
- String withLineBreak()
- String replaceIfEmptyOrNull(String placeholder)
- Widget toText({...})
- String formatAsDate({String format = 'dd MMM, yyyy', String placeholder = ''})
MagicStringExtension
- String forceElapse({required int len, String suffix = "..."})
- String get extractInitials
- TimeOfDay? get toTime
- String toTimeString({String format = 'hh:mm a'})
- String get toHttps
- Color get toStatusColor
MagicNumExtension
- Widget width({Widget? child, Key? key})
- Widget height({Widget? child, Key? key})
- Widget square({Widget? child, Key? key})
MagicTimeExtension
- bool isAfter(TimeOfDay? toTime)
- bool isBefore(TimeOfDay? toTime)
- String format({String placeHolder = '', String format = 'hh:mm a'})
Contributing
Contributions are welcome! Please fork the repository and submit a pull request. For bug reports or feature requests, please open an issue on the GitHub repository.
License
magic_extensions is licensed under the MIT License. See the LICENSE file for details.
Libraries
- magic_extensions
- A library that provides a collection of extension methods for common Dart and Flutter types.