@@ -14,16 +14,9 @@ class ResponsiveValue<T> {
1414
1515 ResponsiveValue (this .context,
1616 {@required this .defaultValue, @required this .valueWhen}) {
17- List <Condition > conditions = valueWhen;
18- conditions.sort ((a, b) => a.breakpoint.compareTo (b.breakpoint));
19- // Get visible value from active condition.
20- value = getValue (context, conditions) ?? defaultValue;
21- }
22-
23- T getValue (BuildContext context, List <Condition > conditions) {
2417 // Breakpoint reference check. Verify a parent
2518 // [ResponsiveWrapper] exists if a reference is found.
26- if (conditions .firstWhere ((element) => element.name != null ,
19+ if (valueWhen .firstWhere ((element) => element.name != null ,
2720 orElse: () => null ) !=
2821 null ) {
2922 try {
@@ -39,6 +32,29 @@ class ResponsiveValue<T> {
3932 }
4033 }
4134
35+ List <Condition > conditions = valueWhen;
36+ List <ResponsiveBreakpointSegment > segments =
37+ ResponsiveWrapper .of (context).breakpointSegments;
38+ conditions = conditions.map ((e) {
39+ if (e.breakpoint == null ) {
40+ return e.copyWith (
41+ breakpoint: segments
42+ .firstWhere (
43+ (element) => element.responsiveBreakpoint.name == e.name,
44+ orElse: () =>
45+ throw ('No breakpoint named `${e .name }` found.' ))
46+ .responsiveBreakpoint
47+ .breakpoint
48+ .toInt ());
49+ }
50+ return e;
51+ }).toList ();
52+ conditions.sort ((a, b) => a.breakpoint.compareTo (b.breakpoint));
53+ // Get visible value from active condition.
54+ value = getValue (context, conditions) ?? defaultValue;
55+ }
56+
57+ T getValue (BuildContext context, List <Condition > conditions) {
4258 // Find the active condition.
4359 Condition activeCondition = getActiveCondition (context, conditions);
4460 // Return active condition value or default value if null.
@@ -103,61 +119,6 @@ class ResponsiveValue<T> {
103119 }
104120}
105121
106- class ResponsiveVisibility extends StatelessWidget {
107- final Widget child;
108- final bool visible;
109- final List <Condition > visibleWhen;
110- final List <Condition > hiddenWhen;
111- final Widget replacement;
112- final bool maintainState;
113- final bool maintainAnimation;
114- final bool maintainSize;
115- final bool maintainSemantics;
116- final bool maintainInteractivity;
117-
118- const ResponsiveVisibility ({
119- Key key,
120- @required this .child,
121- this .visible = true ,
122- this .visibleWhen = const [],
123- this .hiddenWhen = const [],
124- this .replacement = const SizedBox .shrink (),
125- this .maintainState = false ,
126- this .maintainAnimation = false ,
127- this .maintainSize = false ,
128- this .maintainSemantics = false ,
129- this .maintainInteractivity = false ,
130- }) : super (key: key);
131-
132- @override
133- Widget build (BuildContext context) {
134- // Initialize mutable value holders.
135- List <Condition > conditions = [];
136- bool visibleValue = visible;
137-
138- // Combine Conditions.
139- conditions.addAll (visibleWhen? .map ((e) => e.copyWith (value: true )) ?? []);
140- conditions.addAll (hiddenWhen? .map ((e) => e.copyWith (value: false )) ?? []);
141- // Sort by breakpoint value.
142- conditions.sort ((a, b) => a.breakpoint.compareTo (b.breakpoint));
143- // Get visible value from active condition.
144- visibleValue = ResponsiveValue (context,
145- defaultValue: visibleValue, valueWhen: conditions)
146- .value;
147-
148- return Visibility (
149- child: child,
150- replacement: replacement,
151- visible: visibleValue,
152- maintainState: maintainState,
153- maintainAnimation: maintainAnimation,
154- maintainSize: maintainSize,
155- maintainSemantics: maintainSemantics,
156- maintainInteractivity: maintainInteractivity,
157- );
158- }
159- }
160-
161122enum Conditional {
162123 LARGER_THAN ,
163124 EQUALS ,
@@ -170,24 +131,23 @@ class Condition<T> {
170131 final Conditional condition;
171132 final T value;
172133
173- Condition ._({this .breakpoint, this .name, this .condition, this .value})
134+ const Condition ._({this .breakpoint, this .name, this .condition, this .value})
174135 : assert (breakpoint != null || name != null ),
175- assert (breakpoint == null || name == null ),
176136 assert ((condition == Conditional .EQUALS ) ? name != null : true );
177137
178- Condition .equals ({@required String name, T value})
138+ const Condition .equals ({@required String name, T value})
179139 : this .breakpoint = null ,
180140 this .name = name,
181141 this .condition = Conditional .EQUALS ,
182142 this .value = value;
183143
184- Condition .largerThan ({int breakpoint, String name, T value})
144+ const Condition .largerThan ({int breakpoint, String name, T value})
185145 : this .breakpoint = breakpoint,
186146 this .name = name,
187147 this .condition = Conditional .LARGER_THAN ,
188148 this .value = value;
189149
190- Condition .smallerThan ({int breakpoint, String name, T value})
150+ const Condition .smallerThan ({int breakpoint, String name, T value})
191151 : this .breakpoint = breakpoint,
192152 this .name = name,
193153 this .condition = Conditional .SMALLER_THAN ,
@@ -225,3 +185,84 @@ class Condition<T> {
225185 return (a.breakpoint < b.breakpoint) ? - 1 : 1 ;
226186 }
227187}
188+
189+ class ResponsiveVisibility extends StatelessWidget {
190+ final Widget child;
191+ final bool visible;
192+ final List <Condition > visibleWhen;
193+ final List <Condition > hiddenWhen;
194+ final Widget replacement;
195+ final bool maintainState;
196+ final bool maintainAnimation;
197+ final bool maintainSize;
198+ final bool maintainSemantics;
199+ final bool maintainInteractivity;
200+
201+ const ResponsiveVisibility ({
202+ Key key,
203+ @required this .child,
204+ this .visible = true ,
205+ this .visibleWhen = const [],
206+ this .hiddenWhen = const [],
207+ this .replacement = const SizedBox .shrink (),
208+ this .maintainState = false ,
209+ this .maintainAnimation = false ,
210+ this .maintainSize = false ,
211+ this .maintainSemantics = false ,
212+ this .maintainInteractivity = false ,
213+ }) : super (key: key);
214+
215+ @override
216+ Widget build (BuildContext context) {
217+ // Initialize mutable value holders.
218+ List <Condition > conditions = [];
219+ bool visibleValue = visible;
220+
221+ // Combine Conditions.
222+ conditions.addAll (visibleWhen? .map ((e) => e.copyWith (value: true )) ?? []);
223+ conditions.addAll (hiddenWhen? .map ((e) => e.copyWith (value: false )) ?? []);
224+ // Get visible value from active condition.
225+ visibleValue = ResponsiveValue (context,
226+ defaultValue: visibleValue, valueWhen: conditions)
227+ .value;
228+
229+ return Visibility (
230+ child: child,
231+ replacement: replacement,
232+ visible: visibleValue,
233+ maintainState: maintainState,
234+ maintainAnimation: maintainAnimation,
235+ maintainSize: maintainSize,
236+ maintainSemantics: maintainSemantics,
237+ maintainInteractivity: maintainInteractivity,
238+ );
239+ }
240+ }
241+
242+ class ResponsiveConstraints extends StatelessWidget {
243+ final Widget child;
244+ final BoxConstraints constraint;
245+ final List <Condition > constraintsWhen;
246+
247+ const ResponsiveConstraints (
248+ {Key key,
249+ @required this .child,
250+ this .constraint,
251+ this .constraintsWhen = const []})
252+ : super (key: key);
253+
254+ @override
255+ Widget build (BuildContext context) {
256+ // Initialize mutable value holders.
257+ BoxConstraints constraintValue = constraint;
258+ // Get value from active condition.
259+ constraintValue = ResponsiveValue (context,
260+ defaultValue: constraintValue, valueWhen: constraintsWhen)
261+ .value;
262+
263+ return Container (
264+ constraints: constraintValue,
265+ child: child,
266+ );
267+ }
268+ }
0 commit comments