extensions
This project is a starting point to build useful widget extensions for most used widgets such as padding and containers to minimize UI codes
Publishing
dart pub publish --dry-run dart pub publish
🎖 Installation
dependencies:
extensions: "^0.0.2"
Import
import 'package:flutter_extension_kits/flutter_extension_kits.dart';
Usage For Widgets
Text('Line 01').paddingAll(8);
Text('Line 02').paddingOnly(left : 2, top : 4, right : 6, bottom : 8 );
Text('Line 03').paddingSymmetric(horizontal : 10, vertical : 20);
Usage For Variables
int
millisecondsToDate
print(1601996400000.millisecondsToDate()); // DateTime(2020, 10, 7)
double
roundWithDigit
const value1 = 10.45678;
print(value1.roundWithDigit(1)); // 10.5
print(value1.roundWithDigit(2)); // 10.46
print(value1.roundWithDigit(3)); // 10.457
print(value1.roundWithDigit(0)); // 10.45678
print(value1.roundWithDigit(-1)); // 10.45678
const value2 = 10.434321;
print(value2.roundWithDigit(1)); // 10.4
print(value2.roundWithDigit(2); // 10.43
print(value2.roundWithDigit(3)); // 10.434
num
commaFormat
print(100.commaFormat()); // '100'
print(1000.commaFormat()); // '1,000'
print(1000000.commaFormat()); // '1,000,000'
print(1000000.123.commaFormat()); // '1,000,000.123'
String
format
const String text = 'Today is {1} and tomorrow is {0}';
final List<String> placeHolders = List<String>()
..add('Tuesday')
..add('Monday');
final String finalString = text.format(placeHolders);
//'Today is Monday and tomorrow is Tuesday';
toDate
print('20201008'.toDate()); // DateTime(2020, 10, 8)
toInt
print('123'.toInt()); // 123
toDouble
print('123.4567'.toDouble()); // 123.4567
isNewLine
print('\n'.isNewLine()); // true
print('test'.isNewLine()); // false
Date
formatString
final date = DateTime(2020, 10, 7);
print(date.formatString()); // '10.07 2020'
print(date.formatString(format: 'yyyy/MM/dd')); // '2020/10/07'
diffDays, diffHours, diffMinutes, diffSeconds
final dateA = DateTime(2020, 10, 7, 20, 10, 30);
final dateB = DateTime(2020, 8, 2, 10, 5, 10);
final dateC = DateTime(2020, 10, 7, 20, 10, 30);
// dateA - dateB
print(dateA.diffDays(dateB)); // 66
print(dateA.diffHours(dateB)); // 1594
print(dateA.diffMinutes(dateB)); // 95645
print(dateA.diffSeconds(dateB)); // 5738720
Beautify Time Left/Ago
//Time Left
// Current testing time is 2020/05/25 9:11PM
final expiryDate01 = DateTime(2021, 10, 7, 20, 10, 30);
print(expiryDate01.beautifyTime(strSuffix: 'left')); //1 year 5 months left
final expiryDate02 = DateTime(2020, 10, 7, 20, 10, 30);
print(expiryDate02.beautifyTime(strSuffix: 'left')); //4 months left
final expiryDate03 = DateTime(2020, 5, 15, 20, 10, 30);
print(expiryDate03.beautifyTime(strSuffix: 'left')); //10 days left
final expiryDate04 = DateTime(2020, 5, 25, 20, 23, 30);
print(expiryDate04.beautifyTime(strSuffix: 'left')); //46 minutes left
// Current testing time is 2020/05/25 8:53PM
final lastUpdatedDate01 = DateTime(2018, 12, 7, 20, 5, 10);
print(lastUpdatedDate01.beautifyTime(bUTC: false, bAddPlural: true)); //1year 6m
final lastUpdatedDate02 = DateTime(2019, 10, 7, 20, 5, 10);
print(lastUpdatedDate02.beautifyTime(bUTC: false, bAddPlural: true)); //8months
final lastUpdatedDate03 = DateTime(2020, 5, 25, 10, 5, 10);
print(lastUpdatedDate03.beautifyTime(bUTC: false, bAddPlural: true)); //10hours
final lastUpdatedDate04 = DateTime(2020, 5, 25, 20, 5, 10);
print(lastUpdatedDate04.beautifyTime(bUTC: false, bAddPlural: true)); //48minute
final lastUpdatedDate05 = DateTime(2020, 5, 25, 20, 53, 10);
print(lastUpdatedDate05.beautifyTime(bUTC: false, bAddPlural: true)); //Just now
//Time Left
// Current testing time is 2020/05/25 9:11PM
final expiryDate01 = DateTime(2021, 10, 7, 20, 10, 30);
print(expiryDate01.beautifyTime(strSuffix: 'left')); //1 year 5 months left
final expiryDate02 = DateTime(2020, 10, 7, 20, 10, 30);
print(expiryDate02.beautifyTime(strSuffix: 'left')); //4 months left
final expiryDate03 = DateTime(2020, 5, 15, 20, 10, 30);
print(expiryDate03.beautifyTime(strSuffix: 'left')); //10 days left
final expiryDate04 = DateTime(2020, 5, 25, 20, 23, 30);
print(expiryDate04.beautifyTime(strSuffix: 'left')); //46 minutes left
// Current testing time is 2020/05/25 8:53PM
final lastUpdatedDate01 = DateTime(2018, 12, 7, 20, 5, 10);
print(lastUpdatedDate01.beautifyTime(bUTC: false, bAddPlural: true)); //1year 6m
final lastUpdatedDate02 = DateTime(2019, 10, 7, 20, 5, 10);
print(lastUpdatedDate02.beautifyTime(bUTC: false, bAddPlural: true)); //8months
final lastUpdatedDate03 = DateTime(2020, 5, 25, 10, 5, 10);
print(lastUpdatedDate03.beautifyTime(bUTC: false, bAddPlural: true)); //10hours
final lastUpdatedDate04 = DateTime(2020, 5, 25, 20, 5, 10);
print(lastUpdatedDate04.beautifyTime(bUTC: false, bAddPlural: true)); //48minute
final lastUpdatedDate05 = DateTime(2020, 5, 25, 20, 53, 10);
print(lastUpdatedDate05.beautifyTime(bUTC: false, bAddPlural: true)); //Just nows 10hrs
//Time Left
// Current testing time is 2020/05/25 9:11PM
final expiryDate01 = DateTime(2021, 10, 7, 20, 10, 30);
print(expiryDate01.beautifyTime(strSuffix: 'left')); //1 year 5 months left
final expiryDate02 = DateTime(2020, 10, 7, 20, 10, 30);
print(expiryDate02.beautifyTime(strSuffix: 'left')); //4 months left
final expiryDate03 = DateTime(2020, 5, 15, 20, 10, 30);
print(expiryDate03.beautifyTime(strSuffix: 'left')); //10 days left
final expiryDate04 = DateTime(2020, 5, 25, 20, 23, 30);
print(expiryDate04.beautifyTime(strSuffix: 'left')); //46 minutes left
// Current testing time is 2020/05/25 8:53PM
final lastUpdatedDate01 = DateTime(2018, 12, 7, 20, 5, 10);
print(lastUpdatedDate01.beautifyTime(bUTC: false, bAddPlural: true)); //1year 6m
final lastUpdatedDate02 = DateTime(2019, 10, 7, 20, 5, 10);
print(lastUpdatedDate02.beautifyTime(bUTC: false, bAddPlural: true)); //8months
final lastUpdatedDate03 = DateTime(2020, 5, 25, 10, 5, 10);
print(lastUpdatedDate03.beautifyTime(bUTC: false, bAddPlural: true)); //10hours
final lastUpdatedDate04 = DateTime(2020, 5, 25, 20, 5, 10);
print(lastUpdatedDate04.beautifyTime(bUTC: false, bAddPlural: true)); //48minute
final lastUpdatedDate05 = DateTime(2020, 5, 25, 20, 53, 10);
print(lastUpdatedDate05.beautifyTime(bUTC: false, bAddPlural: true)); //Just now
//Time Left
// Current testing time is 2020/05/25 9:11PM
final expiryDate01 = DateTime(2021, 10, 7, 20, 10, 30);
print(expiryDate01.beautifyTime(strSuffix: 'left')); //1 year 5 months left
final expiryDate02 = DateTime(2020, 10, 7, 20, 10, 30);
print(expiryDate02.beautifyTime(strSuffix: 'left')); //4 months left
final expiryDate03 = DateTime(2020, 5, 15, 20, 10, 30);
print(expiryDate03.beautifyTime(strSuffix: 'left')); //10 days left
final expiryDate04 = DateTime(2020, 5, 25, 20, 23, 30);
print(expiryDate04.beautifyTime(strSuffix: 'left')); //46 minutes left
// Current testing time is 2020/05/25 8:53PM
final lastUpdatedDate01 = DateTime(2018, 12, 7, 20, 5, 10);
print(lastUpdatedDate01.beautifyTime(bUTC: false, bAddPlural: true)); //1year 6m
final lastUpdatedDate02 = DateTime(2019, 10, 7, 20, 5, 10);
print(lastUpdatedDate02.beautifyTime(bUTC: false, bAddPlural: true)); //8months
final lastUpdatedDate03 = DateTime(2020, 5, 25, 10, 5, 10);
print(lastUpdatedDate03.beautifyTime(bUTC: false, bAddPlural: true)); //10hours
final lastUpdatedDate04 = DateTime(2020, 5, 25, 20, 5, 10);
print(lastUpdatedDate04.beautifyTime(bUTC: false, bAddPlural: true)); //48minute
final lastUpdatedDate05 = DateTime(2020, 5, 25, 20, 53, 10);
print(lastUpdatedDate05.beautifyTime(bUTC: false, bAddPlural: true)); //Just now
//Time Left
// Current testing time is 2020/05/25 9:11PM
final expiryDate01 = DateTime(2021, 10, 7, 20, 10, 30);
print(expiryDate01.beautifyTime(strSuffix: 'left')); //1 year 5 months left
final expiryDate02 = DateTime(2020, 10, 7, 20, 10, 30);
print(expiryDate02.beautifyTime(strSuffix: 'left')); //4 months left
final expiryDate03 = DateTime(2020, 5, 15, 20, 10, 30);
print(expiryDate03.beautifyTime(strSuffix: 'left')); //10 days left
final expiryDate04 = DateTime(2020, 5, 25, 20, 23, 30);
print(expiryDate04.beautifyTime(strSuffix: 'left')); //46 minutes left
// Current testing time is 2020/05/25 8:53PM
final lastUpdatedDate01 = DateTime(2018, 12, 7, 20, 5, 10);
print(lastUpdatedDate01.beautifyTime(bUTC: false, bAddPlural: true)); //1year 6m
final lastUpdatedDate02 = DateTime(2019, 10, 7, 20, 5, 10);
print(lastUpdatedDate02.beautifyTime(bUTC: false, bAddPlural: true)); //8months
final lastUpdatedDate03 = DateTime(2020, 5, 25, 10, 5, 10);
print(lastUpdatedDate03.beautifyTime(bUTC: false, bAddPlural: true)); //10hours
final lastUpdatedDate04 = DateTime(2020, 5, 25, 20, 5, 10);
print(lastUpdatedDate04.beautifyTime(bUTC: false, bAddPlural: true)); //48minute
final lastUpdatedDate05 = DateTime(2020, 5, 25, 20, 53, 10);
print(lastUpdatedDate05.beautifyTime(bUTC: false, bAddPlural: true)); //Just now 5mins
//Time Left
// Current testing time is 2020/05/25 9:11PM
final expiryDate01 = DateTime(2021, 10, 7, 20, 10, 30);
print(expiryDate01.beautifyTime(strSuffix: 'left')); //1 year 5 months left
final expiryDate02 = DateTime(2020, 10, 7, 20, 10, 30);
print(expiryDate02.beautifyTime(strSuffix: 'left')); //4 months left
final expiryDate03 = DateTime(2020, 5, 15, 20, 10, 30);
print(expiryDate03.beautifyTime(strSuffix: 'left')); //10 days left
final expiryDate04 = DateTime(2020, 5, 25, 20, 23, 30);
print(expiryDate04.beautifyTime(strSuffix: 'left')); //46 minutes left
// Current testing time is 2020/05/25 8:53PM
final lastUpdatedDate01 = DateTime(2018, 12, 7, 20, 5, 10);
print(lastUpdatedDate01.beautifyTime(bUTC: false, bAddPlural: true)); //1year 6m
final lastUpdatedDate02 = DateTime(2019, 10, 7, 20, 5, 10);
print(lastUpdatedDate02.beautifyTime(bUTC: false, bAddPlural: true)); //8months
final lastUpdatedDate03 = DateTime(2020, 5, 25, 10, 5, 10);
print(lastUpdatedDate03.beautifyTime(bUTC: false, bAddPlural: true)); //10hours
final lastUpdatedDate04 = DateTime(2020, 5, 25, 20, 5, 10);
print(lastUpdatedDate04.beautifyTime(bUTC: false, bAddPlural: true)); //48minute
final lastUpdatedDate05 = DateTime(2020, 5, 25, 20, 53, 10);
print(lastUpdatedDate05.beautifyTime(bUTC: false, bAddPlural: true)); //Just now
//Time Left
// Current testing time is 2020/05/25 9:11PM
final expiryDate01 = DateTime(2021, 10, 7, 20, 10, 30);
print(expiryDate01.beautifyTime(strSuffix: 'left')); //1 year 5 months left
final expiryDate02 = DateTime(2020, 10, 7, 20, 10, 30);
print(expiryDate02.beautifyTime(strSuffix: 'left')); //4 months left
final expiryDate03 = DateTime(2020, 5, 15, 20, 10, 30);
print(expiryDate03.beautifyTime(strSuffix: 'left')); //10 days left
final expiryDate04 = DateTime(2020, 5, 25, 20, 23, 30);
print(expiryDate04.beautifyTime(strSuffix: 'left')); //46 minutes left
// Current testing time is 2020/05/25 8:53PM
final lastUpdatedDate01 = DateTime(2018, 12, 7, 20, 5, 10);
print(lastUpdatedDate01.beautifyTime(bUTC: false, bAddPlural: true)); //1year 6m
final lastUpdatedDate02 = DateTime(2019, 10, 7, 20, 5, 10);
print(lastUpdatedDate02.beautifyTime(bUTC: false, bAddPlural: true)); //8months
final lastUpdatedDate03 = DateTime(2020, 5, 25, 10, 5, 10);
print(lastUpdatedDate03.beautifyTime(bUTC: false, bAddPlural: true)); //10hours
final lastUpdatedDate04 = DateTime(2020, 5, 25, 20, 5, 10);
print(lastUpdatedDate04.beautifyTime(bUTC: false, bAddPlural: true)); //48minute
final lastUpdatedDate05 = DateTime(2020, 5, 25, 20, 53, 10);
print(lastUpdatedDate05.beautifyTime(bUTC: false, bAddPlural: true)); //Just now
isCompare
// dateA >= dateB => true
// dateA < dateB => false
final dateA = DateTime(2020, 10, 7, 20, 10, 30);
final dateB = DateTime(2020, 8, 2, 10, 5, 10);
final dateC = DateTime(2020, 10, 7, 20, 10, 30);
print(dateA.isCompare(dateB)); // true
print(dateA.isCompare(dateC)); // true
print(dateB.isCompare(dateA)); // false
List
max
print([10, 2, 3].max()); // 10
print([0.3, 1.2, 0.8].max()); // 1.2
Keybased event manager
var someEvent = ExtendedEvent();
someEvent.subscribe('key01', (args) {
print('Event triggered');
});
print('Triggering event');
someEvent.broadcast();
print('Total subscribers : ' + someEvent.subscriberCount.toString());
bool bUnsubscribeStatus = someEvent.unsubscribe('key02');
if (!bUnsubscribeStatus)
print('Failed to unsubscribe event');
else
print('key02 unsubscribed from the manager');
bUnsubscribeStatus = someEvent.unsubscribe('key01');
if (!bUnsubscribeStatus)
print('Failed to unsubscribe event');
else
print('key01 unsubscribed from the manager');
//Trigger won't reived anymore since we've unsubscribed from the manager
someEvent.broadcast();
print('Total subscribers : ' + someEvent.subscriberCount.toString());
var someEvent = ExtendedEvent();
someEvent.subscribe('key01', (args) {
print('Event triggered');
});
print('Triggering event');
someEvent.broadcast();
print('Total subscribers : ' + someEvent.subscriberCount.toString());
bool bUnsubscribeStatus = someEvent.unsubscribe('key02');
if (!bUnsubscribeStatus)
print('Failed to unsubscribe event');
else
print('key02 unsubscribed from the manager');
bUnsubscribeStatus = someEvent.unsubscribe('key01');
if (!bUnsubscribeStatus)
print('Failed to unsubscribe event');
else
print('key01 unsubscribed from the manager');
//Trigger won't reived anymore since we've unsubscribed from the manager
someEvent.broadcast();
print('Total subscribers : ' + someEvent.subscriberCount.toString());: 8 );
var someEvent = ExtendedEvent();
someEvent.subscribe('key01', (args) {
print('Event triggered');
});
print('Triggering event');
someEvent.broadcast();
print('Total subscribers : ' + someEvent.subscriberCount.toString());
bool bUnsubscribeStatus = someEvent.unsubscribe('key02');
if (!bUnsubscribeStatus)
print('Failed to unsubscribe event');
else
print('key02 unsubscribed from the manager');
bUnsubscribeStatus = someEvent.unsubscribe('key01');
if (!bUnsubscribeStatus)
print('Failed to unsubscribe event');
else
print('key01 unsubscribed from the manager');
//Trigger won't reived anymore since we've unsubscribed from the manager
someEvent.broadcast();
print('Total subscribers : ' + someEvent.subscriberCount.toString());