forked from Codelessly/ResponsiveFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponsive_value.dart
307 lines (268 loc) · 9.33 KB
/
responsive_value.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// ignore_for_file: constant_identifier_names
import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/widgets.dart';
import 'responsive_framework.dart';
/// Conditional values based on the active breakpoint.
///
/// Get a [value] that corresponds to active breakpoint
/// determined by [Condition]s set in [conditionalValues].
/// Set a [value] for when no condition is
/// active. Requires a parent [context] that contains
/// a [ResponsiveBreakpoints].
///
/// No validation is performed on [Condition]s so
/// valid conditions must be passed.
class ResponsiveValue<T> {
T? value;
final T? defaultValue;
final List<Condition<T>> conditionalValues;
final BuildContext context;
ResponsiveValue(this.context,
{required this.conditionalValues, this.defaultValue}) {
// Breakpoint reference check. Verify a parent
// [ResponsiveWrapper] exists if a reference is found.
if (conditionalValues.firstWhereOrNull((element) => element.name != null) !=
null) {
try {
ResponsiveBreakpoints.of(context);
} catch (e) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'A conditional value was caught referencing a nonexistent breakpoint.'),
ErrorDescription(
'ResponsiveValue requires a parent ResponsiveWrapper '
'to reference breakpoints. Add a ResponsiveWrapper or remove breakpoint references.')
]);
}
}
List<Condition> conditions = [];
conditions.addAll(conditionalValues);
// Get visible value from active condition.
value = getValue(context, conditions) ?? defaultValue;
}
T? getValue(BuildContext context, List<Condition> conditions) {
// Find the active condition.
Condition? activeCondition = getActiveCondition(context, conditions);
if (activeCondition == null) return null;
// Return landscape value if orientation is landscape and landscape override value is provided.
if (ResponsiveBreakpoints.of(context).orientation ==
Orientation.landscape &&
activeCondition.landscapeValue != null) {
return activeCondition.landscapeValue;
}
// Return active condition value or default value if null.
return activeCondition.value;
}
/// Set [activeCondition].
/// The active condition is found by matching the
/// search criteria in order of precedence:
/// 1. [Conditional.EQUALS]
/// Named breakpoints from a parent [ResponsiveBreakpoints].
/// 2. [Conditional.BETWEEN]
/// 3. [Conditional.SMALLER_THAN]
/// a. Named breakpoints.
/// b. Unnamed breakpoints.
/// 4. [Conditional.LARGER_THAN]
/// a. Named breakpoints.
/// b. Unnamed breakpoints.
/// Returns null if no Active Condition is found.
Condition? getActiveCondition(
BuildContext context, List<Condition> conditions) {
ResponsiveBreakpointsData responsiveWrapperData =
ResponsiveBreakpoints.of(context);
double screenWidth = responsiveWrapperData.screenWidth;
for (Condition condition in conditions.reversed) {
if (condition.condition == Conditional.EQUALS) {
if (condition.name == responsiveWrapperData.breakpoint.name) {
return condition;
}
continue;
}
if (condition.condition == Conditional.BETWEEN) {
if (screenWidth >= condition.breakpointStart! &&
screenWidth <= condition.breakpointEnd!) {
return condition;
}
continue;
}
if (condition.condition == Conditional.SMALLER_THAN) {
if (condition.name != null) {
if (responsiveWrapperData.smallerThan(condition.name!)) {
return condition;
}
}
if (condition.breakpointStart != null) {
if (screenWidth < condition.breakpointStart!) {
return condition;
}
}
continue;
}
if (condition.condition == Conditional.LARGER_THAN) {
if (condition.name != null) {
if (responsiveWrapperData.largerThan(condition.name!)) {
return condition;
}
}
if (condition.breakpointStart != null) {
if (screenWidth > condition.breakpointStart!) {
return condition;
}
}
continue;
}
}
return null;
}
}
/// Internal equality comparators.
enum Conditional {
LARGER_THAN,
EQUALS,
SMALLER_THAN,
BETWEEN,
}
/// A conditional value provider.
///
/// Provides the [value] when the [condition] is active.
/// Compare conditions by setting either [breakpoint] or
/// [name] values.
class Condition<T> {
final int? breakpointStart;
final int? breakpointEnd;
final String? name;
final Conditional? condition;
final T? value;
final T? landscapeValue;
const Condition._(
{this.breakpointStart,
this.breakpointEnd,
this.name,
this.condition,
this.value,
this.landscapeValue})
: assert(breakpointStart != null || name != null),
assert((condition == Conditional.EQUALS) ? name != null : true);
const Condition.equals({required this.name, this.value, this.landscapeValue})
: breakpointStart = null,
breakpointEnd = null,
condition = Conditional.EQUALS;
const Condition.largerThan(
{int? breakpoint, this.name, this.value, this.landscapeValue})
: breakpointStart = breakpoint,
breakpointEnd = breakpoint,
condition = Conditional.LARGER_THAN;
const Condition.smallerThan(
{int? breakpoint, this.name, this.value, this.landscapeValue})
: breakpointStart = breakpoint,
breakpointEnd = breakpoint,
condition = Conditional.SMALLER_THAN;
/// Conditional when screen width is between [start] and [end] inclusive.
const Condition.between(
{required int? start, required int? end, this.value, this.landscapeValue})
: breakpointStart = start,
breakpointEnd = end,
name = null,
condition = Conditional.BETWEEN;
Condition copyWith({
int? breakpointStart,
int? breakpointEnd,
String? name,
Conditional? condition,
T? value,
T? landscapeValue,
}) =>
Condition._(
breakpointStart: breakpointStart ?? this.breakpointStart,
breakpointEnd: breakpointEnd ?? this.breakpointEnd,
name: name ?? this.name,
condition: condition ?? this.condition,
value: value ?? this.value,
landscapeValue: landscapeValue ?? this.landscapeValue,
);
@override
String toString() =>
'Condition(breakpointStart: $breakpointStart, breakpointEnd: $breakpointEnd, name: $name, condition: $condition, value: $value, landscapeValue: $landscapeValue)';
int sort(Condition a, Condition b) {
if (a.breakpointStart == b.breakpointStart) return 0;
return (a.breakpointStart! < b.breakpointStart!) ? -1 : 1;
}
}
/// A convenience wrapper for responsive [Visibility].
///
/// ResponsiveVisibility accepts [Condition]s in
/// [visibleConditions] and [hiddenConditions] convenience
/// fields. The [child] widget is [visible] by default.
class ResponsiveVisibility extends StatelessWidget {
final Widget child;
final bool visible;
final List<Condition> visibleConditions;
final List<Condition> hiddenConditions;
final Widget replacement;
final bool maintainState;
final bool maintainAnimation;
final bool maintainSize;
final bool maintainSemantics;
final bool maintainInteractivity;
const ResponsiveVisibility({
Key? key,
required this.child,
this.visible = true,
this.visibleConditions = const [],
this.hiddenConditions = const [],
this.replacement = const SizedBox.shrink(),
this.maintainState = false,
this.maintainAnimation = false,
this.maintainSize = false,
this.maintainSemantics = false,
this.maintainInteractivity = false,
}) : super(key: key);
@override
Widget build(BuildContext context) {
// Initialize mutable value holders.
List<Condition> conditions = [];
bool? visibleValue = visible;
// Combine Conditions.
conditions.addAll(visibleConditions.map((e) => e.copyWith(value: true)));
conditions.addAll(hiddenConditions.map((e) => e.copyWith(value: false)));
// Get visible value from active condition.
visibleValue = ResponsiveValue(context,
defaultValue: visibleValue, conditionalValues: conditions)
.value;
return Visibility(
replacement: replacement,
visible: visibleValue!,
maintainState: maintainState,
maintainAnimation: maintainAnimation,
maintainSize: maintainSize,
maintainSemantics: maintainSemantics,
maintainInteractivity: maintainInteractivity,
child: child,
);
}
}
class ResponsiveConstraints extends StatelessWidget {
final Widget child;
final BoxConstraints? constraint;
final List<Condition> conditionalConstraints;
const ResponsiveConstraints(
{Key? key,
required this.child,
this.constraint,
this.conditionalConstraints = const []})
: super(key: key);
@override
Widget build(BuildContext context) {
// Initialize mutable value holders.
BoxConstraints? constraintValue = constraint;
// Get value from active condition.
constraintValue = ResponsiveValue(context,
defaultValue: constraintValue,
conditionalValues: conditionalConstraints)
.value;
return Container(
constraints: constraintValue,
child: child,
);
}
}