Skip to content

Commit 6d6f690

Browse files
committed
ResponsiveValue Between Condition Creation
- Create Between conditional.
1 parent a39615f commit 6d6f690

1 file changed

Lines changed: 47 additions & 19 deletions

File tree

lib/responsive_value.dart

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ class ResponsiveValue<T> {
6666
/// search criteria in order of precedence:
6767
/// 1. [Conditional.EQUALS]
6868
/// Named breakpoints from a parent [ResponsiveBreakpoints].
69-
/// 2. [Conditional.SMALLER_THAN]
69+
/// 2. [Conditional.BETWEEN]
70+
/// 3. [Conditional.SMALLER_THAN]
7071
/// a. Named breakpoints.
7172
/// b. Unnamed breakpoints.
72-
/// 3. [Conditional.LARGER_THAN]
73+
/// 4. [Conditional.LARGER_THAN]
7374
/// a. Named breakpoints.
7475
/// b. Unnamed breakpoints.
7576
/// Returns null if no Active Condition is found.
@@ -88,15 +89,24 @@ class ResponsiveValue<T> {
8889
continue;
8990
}
9091

92+
if (condition.condition == Conditional.BETWEEN) {
93+
if (screenWidth >= condition.breakpointStart! &&
94+
screenWidth <= condition.breakpointEnd!) {
95+
return condition;
96+
}
97+
98+
continue;
99+
}
100+
91101
if (condition.condition == Conditional.SMALLER_THAN) {
92102
if (condition.name != null) {
93103
if (responsiveWrapperData.isSmallerThan(condition.name!)) {
94104
return condition;
95105
}
96106
}
97107

98-
if (condition.breakpoint != null) {
99-
if (screenWidth < condition.breakpoint!) {
108+
if (condition.breakpointStart != null) {
109+
if (screenWidth < condition.breakpointStart!) {
100110
return condition;
101111
}
102112
}
@@ -111,8 +121,8 @@ class ResponsiveValue<T> {
111121
}
112122
}
113123

114-
if (condition.breakpoint != null) {
115-
if (screenWidth > condition.breakpoint!) {
124+
if (condition.breakpointStart != null) {
125+
if (screenWidth > condition.breakpointStart!) {
116126
return condition;
117127
}
118128
}
@@ -130,6 +140,7 @@ enum Conditional {
130140
LARGER_THAN,
131141
EQUALS,
132142
SMALLER_THAN,
143+
BETWEEN,
133144
}
134145

135146
/// A conditional value provider.
@@ -138,42 +149,59 @@ enum Conditional {
138149
/// Compare conditions by setting either [breakpoint] or
139150
/// [name] values.
140151
class Condition<T> {
141-
final int? breakpoint;
152+
final int? breakpointStart;
153+
final int? breakpointEnd;
142154
final String? name;
143155
final Conditional? condition;
144156
final T? value;
145157
final T? landscapeValue;
146158

147159
const Condition._(
148-
{this.breakpoint,
160+
{this.breakpointStart,
161+
this.breakpointEnd,
149162
this.name,
150163
this.condition,
151164
this.value,
152165
this.landscapeValue})
153-
: assert(breakpoint != null || name != null),
166+
: assert(breakpointStart != null || name != null),
154167
assert((condition == Conditional.EQUALS) ? name != null : true);
155168

156169
const Condition.equals({required this.name, this.value, this.landscapeValue})
157-
: breakpoint = null,
170+
: breakpointStart = null,
171+
breakpointEnd = null,
158172
condition = Conditional.EQUALS;
159173

160174
const Condition.largerThan(
161-
{this.breakpoint, this.name, this.value, this.landscapeValue})
162-
: condition = Conditional.LARGER_THAN;
175+
{int? breakpoint, this.name, this.value, this.landscapeValue})
176+
: breakpointStart = breakpoint,
177+
breakpointEnd = breakpoint,
178+
condition = Conditional.LARGER_THAN;
163179

164180
const Condition.smallerThan(
165-
{this.breakpoint, this.name, this.value, this.landscapeValue})
166-
: condition = Conditional.SMALLER_THAN;
181+
{int? breakpoint, this.name, this.value, this.landscapeValue})
182+
: breakpointStart = breakpoint,
183+
breakpointEnd = breakpoint,
184+
condition = Conditional.SMALLER_THAN;
185+
186+
/// Conditional when screen width is between [start] and [end] inclusive.
187+
const Condition.between(
188+
{required int? start, required int? end, this.value, this.landscapeValue})
189+
: breakpointStart = start,
190+
breakpointEnd = end,
191+
name = null,
192+
condition = Conditional.BETWEEN;
167193

168194
Condition copyWith({
169-
int? breakpoint,
195+
int? breakpointStart,
196+
int? breakpointEnd,
170197
String? name,
171198
Conditional? condition,
172199
T? value,
173200
T? landscapeValue,
174201
}) =>
175202
Condition._(
176-
breakpoint: breakpoint ?? this.breakpoint,
203+
breakpointStart: breakpointStart ?? this.breakpointStart,
204+
breakpointEnd: breakpointEnd ?? this.breakpointEnd,
177205
name: name ?? this.name,
178206
condition: condition ?? this.condition,
179207
value: value ?? this.value,
@@ -182,12 +210,12 @@ class Condition<T> {
182210

183211
@override
184212
String toString() =>
185-
'Condition(breakpoint: $breakpoint, name: $name, condition: $condition, value: $value, landscapeValue: $landscapeValue)';
213+
'Condition(breakpointStart: $breakpointStart, breakpointEnd: $breakpointEnd, name: $name, condition: $condition, value: $value, landscapeValue: $landscapeValue)';
186214

187215
int sort(Condition a, Condition b) {
188-
if (a.breakpoint == b.breakpoint) return 0;
216+
if (a.breakpointStart == b.breakpointStart) return 0;
189217

190-
return (a.breakpoint! < b.breakpoint!) ? -1 : 1;
218+
return (a.breakpointStart! < b.breakpointStart!) ? -1 : 1;
191219
}
192220
}
193221

0 commit comments

Comments
 (0)