@@ -11,7 +11,7 @@ class ResponsiveValue<T> {
1111 String smallerThan = '<' ;
1212}
1313
14- class ResponsiveVisibility extends StatefulWidget {
14+ class ResponsiveVisibility extends StatelessWidget {
1515 final Widget child;
1616 final bool visible;
1717 final List <Condition > visibleWhen;
@@ -27,8 +27,8 @@ class ResponsiveVisibility extends StatefulWidget {
2727 Key key,
2828 @required this .child,
2929 this .visible = true ,
30- this .visibleWhen,
31- this .hiddenWhen,
30+ this .visibleWhen = const [] ,
31+ this .hiddenWhen = const [] ,
3232 this .replacement = const SizedBox .shrink (),
3333 this .maintainState = false ,
3434 this .maintainAnimation = false ,
@@ -37,41 +37,6 @@ class ResponsiveVisibility extends StatefulWidget {
3737 this .maintainInteractivity = false ,
3838 }) : super (key: key);
3939
40- @override
41- _ResponsiveVisibilityState createState () => _ResponsiveVisibilityState ();
42- }
43-
44- class _ResponsiveVisibilityState extends State <ResponsiveVisibility >
45- with WidgetsBindingObserver {
46- List <Condition > conditions = [];
47- Condition activeCondition;
48- bool visibleValue;
49-
50- void setDimensions () {
51- // Breakpoint reference check. Verify a parent
52- // [ResponsiveWrapper] exists if a reference is found.
53- if (conditions.firstWhere ((element) => element.name != null ,
54- orElse: () => null ) !=
55- null ) {
56- try {
57- ResponsiveWrapper .of (context);
58- } catch (e) {
59- throw FlutterError .fromParts (< DiagnosticsNode > [
60- ErrorSummary (
61- 'A ResponsiveCondition was caught referencing a nonexistant breakpoint.' ),
62- ErrorDescription (
63- 'A ResponsiveCondition requires a parent ResponsiveWrapper '
64- 'to reference breakpoints. Add a ResponsiveWrapper or remove breakpoint references.' )
65- ]);
66- }
67- }
68-
69- // Find the active condition.
70- activeCondition = getActiveCondition ();
71- // Set value to active condition value or default value if null.
72- visibleValue = activeCondition? .value ?? widget.visible;
73- }
74-
7540 /// Set [activeCondition] .
7641 /// The active condition is found by matching the
7742 /// search criteria in order of precedence:
@@ -84,7 +49,8 @@ class _ResponsiveVisibilityState extends State<ResponsiveVisibility>
8449 /// a. Named breakpoints.
8550 /// b. Unnamed breakpoints.
8651 /// Returns null if no Active Condition is found.
87- Condition getActiveCondition () {
52+ Condition getActiveCondition (
53+ BuildContext context, List <Condition > conditions) {
8854 Condition equalsCondition = conditions.firstWhere ((element) {
8955 if (element.condition == Conditional .EQUALS ) {
9056 return ResponsiveWrapper .of (context).activeBreakpoint? .name ==
@@ -128,59 +94,55 @@ class _ResponsiveVisibilityState extends State<ResponsiveVisibility>
12894 return null ;
12995 }
13096
131- @override
132- void initState () {
133- super .initState ();
134- // Initialize value.
135- visibleValue = widget.visible;
136- // Combine [ResponsiveCondition]s.
137- conditions
138- .addAll (widget.visibleWhen? .map ((e) => e.copyWith (value: true )) ?? []);
139- conditions
140- .addAll (widget.hiddenWhen? .map ((e) => e.copyWith (value: false )) ?? []);
141- // Sort by breakpoint value.
142- conditions.sort ((a, b) => a.breakpoint.compareTo (b.breakpoint));
143-
144- WidgetsBinding .instance.addObserver (this );
145- WidgetsBinding .instance.addPostFrameCallback ((_) {
146- setDimensions ();
147- setState (() {});
148- });
149- }
150-
151- @override
152- void dispose () {
153- WidgetsBinding .instance.removeObserver (this );
154- super .dispose ();
155- }
156-
157- @override
158- void didChangeMetrics () {
159- super .didChangeMetrics ();
160- WidgetsBinding .instance.addPostFrameCallback ((_) {
161- setDimensions ();
162- setState (() {});
163- });
164- }
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+ }
165115
166- @override
167- void didUpdateWidget (ResponsiveVisibility oldWidget) {
168- super .didUpdateWidget (oldWidget);
169- setDimensions ();
170- setState (() {});
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;
171120 }
172121
173122 @override
174123 Widget build (BuildContext context) {
124+ // Initialize mutable value holders.
125+ List <Condition > conditions = [];
126+ Condition activeCondition;
127+ bool visibleValue = visible;
128+
129+ // Combine Conditions.
130+ conditions.addAll (visibleWhen? .map ((e) => e.copyWith (value: true )) ?? []);
131+ conditions.addAll (hiddenWhen? .map ((e) => e.copyWith (value: false )) ?? []);
132+ // Sort by breakpoint value.
133+ conditions.sort ((a, b) => a.breakpoint.compareTo (b.breakpoint));
134+ // Get visible value from active condition.
135+ visibleValue = getVisibleValue (context, conditions);
136+
175137 return Visibility (
176- child: widget. child,
177- replacement: widget. replacement,
138+ child: child,
139+ replacement: replacement,
178140 visible: visibleValue,
179- maintainState: widget. maintainState,
180- maintainAnimation: widget. maintainAnimation,
181- maintainSize: widget. maintainSize,
182- maintainSemantics: widget. maintainSemantics,
183- maintainInteractivity: widget. maintainInteractivity,
141+ maintainState: maintainState,
142+ maintainAnimation: maintainAnimation,
143+ maintainSize: maintainSize,
144+ maintainSemantics: maintainSemantics,
145+ maintainInteractivity: maintainInteractivity,
184146 );
185147 }
186148}
@@ -209,13 +171,13 @@ class Condition {
209171 this .value = value;
210172
211173 Condition .largerThan ({int breakpoint, String name, bool value})
212- : this .breakpoint = breakpoint ?? double .infinity ,
174+ : this .breakpoint = breakpoint,
213175 this .name = name,
214176 this .condition = Conditional .LARGER_THAN ,
215177 this .value = value;
216178
217179 Condition .smallerThan ({int breakpoint, String name, bool value})
218- : this .breakpoint = breakpoint ?? double .negativeInfinity ,
180+ : this .breakpoint = breakpoint,
219181 this .name = name,
220182 this .condition = Conditional .SMALLER_THAN ,
221183 this .value = value;
0 commit comments