-
Notifications
You must be signed in to change notification settings - Fork 0
/
flutter_extensions.dart
183 lines (158 loc) · 4.99 KB
/
flutter_extensions.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
/// This file contains useful Flutter extensions project:
///
/// - [CustomCases] for converting "camelCase" to "Spaced Title Case".
///
/// - [SizedBoxFromDouble] for creating [SizedBox] with a double value.
///
/// - [DurationFromInt] for creating [Duration] with an integer value.
///
/// - [PaddingFromDouble] for creating [EdgeInsets] with a double value.
///
/// - [PaddingFromListDouble] for creating [EdgeInsets] with a (list of) double
/// value(s).
///
/// - [AlignmentUtils] for checking the alignment of an [Alignment] object.
///
/// - [ValidAsyncSnapshot] for checking the state of an [AsyncSnapshot].
///
/// - [AsyncSnapshotErrorToString] for getting the error message from an
/// [AsyncSnapshot].
///
/// - [LocaleProviderFromContext] for getting the [LocaleProvider] from the
/// context.
///
/// - [TodayDateWithHours] for getting the current date with a specific hour.
///
/// - [SameHourMinuteSecond] for checking if two [DateTime] objects have the
/// same hour, minute, or second.
extension CustomCases on String {
String get camelCaseToSpacedTitleCase {
return split(RegExp("(?=[A-Z])"))
.map((word) => "${word[0].toUpperCase()}${word.substring(1)}")
.join(" ");
}
}
extension SizedBoxFromDouble on double {
get verticalSpace => SizedBox(height: this);
get horizontalSpace => SizedBox(width: this);
}
extension DurationFromInt on int {
get seconds => Duration(seconds: this);
get milliseconds => Duration(milliseconds: this);
}
extension PaddingFromDouble on double {
get all => EdgeInsets.all(this);
get horizontal => EdgeInsets.symmetric(horizontal: this);
get vertical => EdgeInsets.symmetric(vertical: this);
get onlyBottom => EdgeInsets.only(bottom: this);
get onlyTop => EdgeInsets.only(top: this);
get onlyRight => EdgeInsets.only(right: this);
get onlyLeft => EdgeInsets.only(left: this);
}
extension PaddingFromListDouble on List<double> {
get fromLTRB {
if (length == 4) {
return EdgeInsets.fromLTRB(this[0], this[1], this[2], this[3]);
}
// TODO Add exception
return EdgeInsets.zero;
}
get verticalHorizontal {
if (length == 2) {
return EdgeInsets.symmetric(vertical: this[0], horizontal: this[1]);
}
// TODO Add exception
return EdgeInsets.zero;
}
}
extension AlignmentUtils on Alignment {
/* Vertical */
bool get isTop => y == -1.0;
bool get isVerticalCenter => y == 0.0;
bool get isBottom => y == 1.0;
/* Horizontal */
bool get isLeft => x == -1.0;
bool get isHorizontalCenter => x == 0.0;
bool get isRight => x == 1.0;
bool get isTopLeft => isTop && isLeft;
bool get isTopCenter => isTop && isHorizontalCenter;
bool get isTopRight => isTop && isRight;
bool get isCenterLeft => isVerticalCenter && isLeft;
bool get isCenter => isVerticalCenter && isHorizontalCenter;
bool get isCenterRight => isVerticalCenter && isRight;
bool get isBottomLeft => isBottom && isLeft;
bool get isBottomCenter => isBottom && isHorizontalCenter;
bool get isBottomRight => isBottom && isRight;
double get vOffset {
return y;
// OR
if (isTop) return -1.0; // y
if (isVerticalCenter) return 0.0; // y
if (isBottom) return 1.0; // y
return 1.0;
}
double get hOffset {
return x;
// OR
if (isLeft) return -1.0; // x
if (isHorizontalCenter) return 0.0; // x
if (isRight) return 1.0; // x
return 1.0;
}
String get name {
if (isTopLeft) return "Top Left";
if (isTopCenter) return "Top Center";
if (isTopRight) return "Top Right";
if (isCenterLeft) return "Center Left";
if (isCenter) return "Center";
if (isCenterRight) return "Center Right";
if (isBottomLeft) return "Bottom Left";
if (isBottomCenter) return "Bottom Center";
if (isBottomRight) return "Bottom Right";
throw Exception("Cannot detect the correct alignment.");
}
}
extension ValidAsyncSnapshot on AsyncSnapshot {
bool get isDone => connectionState == ConnectionState.done;
bool get isWaiting => connectionState == ConnectionState.waiting;
bool get isValid => isDone && hasData;
}
extension AsyncSnapshotErrorToString on AsyncSnapshot {
String? get errorMessage => error?.toString();
}
extension LocaleProviderFromContext on BuildContext {
LocaleProvider get localeProvider => Provider.of<LocaleProvider>(
this,
listen: false,
);
Locale get currentLocale => localeProvider.currentLocale;
}
extension TodayDateWithHours on int {
DateTime get todayAt {
final now = DateTime.now();
return DateTime(
now.year,
now.month,
now.day,
this, // The hour.
0,
0,
);
}
}
extension SameHourMinuteSecond on DateTime {
bool sameHour(DateTime dt) {
return hour == dt.hour;
}
bool sameMinute(DateTime dt) {
return minute == dt.minute;
}
bool sameSecond(DateTime dt) {
return second == dt.second;
}
bool sameTime(DateTime dt) {
return sameSecond(dt) && sameMinute(dt) && sameHour(dt);
}
}