@@ -7,35 +7,43 @@ typedef StartListening<T> = VoidCallback Function(Value<T> element, T value);
77
88class ResponsiveValue <T > {
99 T value;
10- String greaterThan = '>' ;
11- String smallerThan = '<' ;
12- }
10+ final T defaultValue;
11+ final List <Condition > valueWhen;
1312
14- class ResponsiveVisibility extends StatelessWidget {
15- final Widget child;
16- final bool visible;
17- final List <Condition > visibleWhen;
18- final List <Condition > hiddenWhen;
19- final Widget replacement;
20- final bool maintainState;
21- final bool maintainAnimation;
22- final bool maintainSize;
23- final bool maintainSemantics;
24- final bool maintainInteractivity;
13+ final BuildContext context;
2514
26- const ResponsiveVisibility ({
27- Key key,
28- @required this .child,
29- this .visible = true ,
30- this .visibleWhen = const [],
31- this .hiddenWhen = const [],
32- this .replacement = const SizedBox .shrink (),
33- this .maintainState = false ,
34- this .maintainAnimation = false ,
35- this .maintainSize = false ,
36- this .maintainSemantics = false ,
37- this .maintainInteractivity = false ,
38- }) : super (key: key);
15+ ResponsiveValue (this .context,
16+ {@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) {
24+ // Breakpoint reference check. Verify a parent
25+ // [ResponsiveWrapper] exists if a reference is found.
26+ if (conditions.firstWhere ((element) => element.name != null ,
27+ orElse: () => null ) !=
28+ null ) {
29+ try {
30+ ResponsiveWrapper .of (context);
31+ } catch (e) {
32+ throw FlutterError .fromParts (< DiagnosticsNode > [
33+ ErrorSummary (
34+ 'A ResponsiveCondition was caught referencing a nonexistant breakpoint.' ),
35+ ErrorDescription (
36+ 'A ResponsiveCondition requires a parent ResponsiveWrapper '
37+ 'to reference breakpoints. Add a ResponsiveWrapper or remove breakpoint references.' )
38+ ]);
39+ }
40+ }
41+
42+ // Find the active condition.
43+ Condition activeCondition = getActiveCondition (context, conditions);
44+ // Return active condition value or default value if null.
45+ return activeCondition? .value;
46+ }
3947
4048 /// Set [activeCondition] .
4149 /// The active condition is found by matching the
@@ -93,37 +101,38 @@ class ResponsiveVisibility extends StatelessWidget {
93101
94102 return null ;
95103 }
104+ }
96105
97- bool getVisibleValue (BuildContext context, List <Condition > conditions) {
98- // Breakpoint reference check. Verify a parent
99- // [ResponsiveWrapper] exists if a reference is found.
100- if (conditions.firstWhere ((element) => element.name != null ,
101- orElse: () => null ) !=
102- null ) {
103- try {
104- ResponsiveWrapper .of (context);
105- } catch (e) {
106- throw FlutterError .fromParts (< DiagnosticsNode > [
107- ErrorSummary (
108- 'A ResponsiveCondition was caught referencing a nonexistant breakpoint.' ),
109- ErrorDescription (
110- 'A ResponsiveCondition requires a parent ResponsiveWrapper '
111- 'to reference breakpoints. Add a ResponsiveWrapper or remove breakpoint references.' )
112- ]);
113- }
114- }
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;
115117
116- // Find the active condition.
117- Condition activeCondition = getActiveCondition (context, conditions);
118- // Return active condition value or default value if null.
119- return activeCondition? .value ?? visible;
120- }
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);
121131
122132 @override
123133 Widget build (BuildContext context) {
124134 // Initialize mutable value holders.
125135 List <Condition > conditions = [];
126- Condition activeCondition;
127136 bool visibleValue = visible;
128137
129138 // Combine Conditions.
@@ -132,7 +141,9 @@ class ResponsiveVisibility extends StatelessWidget {
132141 // Sort by breakpoint value.
133142 conditions.sort ((a, b) => a.breakpoint.compareTo (b.breakpoint));
134143 // Get visible value from active condition.
135- visibleValue = getVisibleValue (context, conditions);
144+ visibleValue = ResponsiveValue (context,
145+ defaultValue: visibleValue,
146+ valueWhen: [Condition .smallerThan (name: DESKTOP , value: false )]).value;
136147
137148 return Visibility (
138149 child: child,
@@ -153,30 +164,30 @@ enum Conditional {
153164 SMALLER_THAN ,
154165}
155166
156- class Condition {
167+ class Condition < T > {
157168 final int breakpoint;
158169 final String name;
159170 final Conditional condition;
160- final bool value;
171+ final T value;
161172
162173 Condition ._({this .breakpoint, this .name, this .condition, this .value})
163174 : assert (breakpoint != null || name != null ),
164175 assert (breakpoint == null || name == null ),
165176 assert ((condition == Conditional .EQUALS ) ? name != null : true );
166177
167- Condition .equals (String name, { bool value})
178+ Condition .equals ({ @required String name, @required T value})
168179 : this .breakpoint = null ,
169180 this .name = name,
170181 this .condition = Conditional .EQUALS ,
171182 this .value = value;
172183
173- Condition .largerThan ({int breakpoint, String name, bool value})
184+ Condition .largerThan ({int breakpoint, String name, @required T value})
174185 : this .breakpoint = breakpoint,
175186 this .name = name,
176187 this .condition = Conditional .LARGER_THAN ,
177188 this .value = value;
178189
179- Condition .smallerThan ({int breakpoint, String name, bool value})
190+ Condition .smallerThan ({int breakpoint, String name, @required T value})
180191 : this .breakpoint = breakpoint,
181192 this .name = name,
182193 this .condition = Conditional .SMALLER_THAN ,
0 commit comments