Skip to content

Commit e45723c

Browse files
committed
Landscape Orientation Support #1
- Add support for landscape value override in ResponsiveValue.
1 parent cc8d1a0 commit e45723c

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

lib/responsive_value.dart

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ class ResponsiveValue<T> {
6363
T? getValue(BuildContext context, List<Condition> conditions) {
6464
// Find the active condition.
6565
Condition? activeCondition = getActiveCondition(context, conditions);
66+
if (activeCondition == null) return null;
67+
// Return landscape value if orientation is landscape and landscape override value is provided.
68+
if (ResponsiveWrapper.of(context).orientation == Orientation.landscape &&
69+
activeCondition.landscapeValue != null)
70+
return activeCondition.landscapeValue;
6671
// Return active condition value or default value if null.
67-
return activeCondition?.value;
72+
return activeCondition.value;
6873
}
6974

7075
/// Set [activeCondition].
@@ -143,40 +148,53 @@ class Condition<T> {
143148
final String? name;
144149
final Conditional? condition;
145150
final T? value;
146-
147-
const Condition._({this.breakpoint, this.name, this.condition, this.value})
151+
final T? landscapeValue;
152+
153+
const Condition._(
154+
{this.breakpoint,
155+
this.name,
156+
this.condition,
157+
this.value,
158+
this.landscapeValue})
148159
: assert(breakpoint != null || name != null),
149160
assert((condition == Conditional.EQUALS) ? name != null : true);
150161

151-
const Condition.equals({required String name, T? value})
162+
const Condition.equals({required String name, T? value, T? landscapeValue})
152163
: this.breakpoint = null,
153164
this.name = name,
154165
this.condition = Conditional.EQUALS,
155-
this.value = value;
166+
this.value = value,
167+
this.landscapeValue = landscapeValue;
156168

157-
const Condition.largerThan({int? breakpoint, String? name, T? value})
169+
const Condition.largerThan(
170+
{int? breakpoint, String? name, T? value, T? landscapeValue})
158171
: this.breakpoint = breakpoint,
159172
this.name = name,
160173
this.condition = Conditional.LARGER_THAN,
161-
this.value = value;
174+
this.value = value,
175+
this.landscapeValue = landscapeValue;
162176

163-
const Condition.smallerThan({int? breakpoint, String? name, T? value})
177+
const Condition.smallerThan(
178+
{int? breakpoint, String? name, T? value, T? landscapeValue})
164179
: this.breakpoint = breakpoint,
165180
this.name = name,
166181
this.condition = Conditional.SMALLER_THAN,
167-
this.value = value;
182+
this.value = value,
183+
this.landscapeValue = landscapeValue;
168184

169185
Condition copyWith({
170186
int? breakpoint,
171187
String? name,
172188
Conditional? condition,
173-
bool? value,
189+
T? value,
190+
T? landscapeValue,
174191
}) =>
175192
Condition._(
176193
breakpoint: breakpoint ?? this.breakpoint,
177194
name: name ?? this.name,
178195
condition: condition ?? this.condition,
179196
value: value ?? this.value,
197+
landscapeValue: landscapeValue ?? this.value,
180198
);
181199

182200
@override
@@ -190,6 +208,8 @@ class Condition<T> {
190208
condition.toString() +
191209
', value: ' +
192210
value.toString() +
211+
', landscapeValue: ' +
212+
landscapeValue.toString() +
193213
')';
194214

195215
int sort(Condition a, Condition b) {

lib/responsive_wrapper.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ class ResponsiveWrapperData {
572572
final bool isPhone;
573573
final bool isTablet;
574574
final bool isDesktop;
575+
final Orientation orientation;
575576

576577
/// Creates responsive data with explicit values.
577578
///
@@ -589,6 +590,7 @@ class ResponsiveWrapperData {
589590
this.isPhone = false,
590591
this.isTablet = false,
591592
this.isDesktop = false,
593+
this.orientation = Orientation.portrait,
592594
});
593595

594596
/// Creates data based on the [ResponsiveWrapper] state.
@@ -609,6 +611,9 @@ class ResponsiveWrapperData {
609611
state.activeBreakpointSegment.responsiveBreakpoint.name == TABLET,
610612
isDesktop:
611613
state.activeBreakpointSegment.responsiveBreakpoint.name == DESKTOP,
614+
orientation: state.screenWidth > state.screenHeight
615+
? Orientation.portrait
616+
: Orientation.landscape,
612617
);
613618
}
614619

0 commit comments

Comments
 (0)