1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'basic.dart';
import 'framework.dart';
import 'sliver.dart';
import 'ticker_provider.dart';
/// Whether to show or hide a child.
///
/// By default, the [visible] property controls whether the [child] is included
/// in the subtree or not; when it is not [visible], the [replacement] child
/// (typically a zero-sized box) is included instead.
///
/// A variety of flags can be used to tweak exactly how the child is hidden.
/// (Changing the flags dynamically is discouraged, as it can cause the [child]
/// subtree to be rebuilt, with any state in the subtree being discarded.
/// Typically, only the [visible] flag is changed dynamically.)
///
/// These widgets provide some of the facets of this one:
///
/// * [Opacity], which can stop its child from being painted.
/// * [Offstage], which can stop its child from being laid out or painted.
/// * [TickerMode], which can stop its child from being animated.
/// * [ExcludeSemantics], which can hide the child from accessibility tools.
/// * [IgnorePointer], which can disable touch interactions with the child.
///
/// Using this widget is not necessary to hide children. The simplest way to
/// hide a child is just to not include it, or, if a child _must_ be given (e.g.
/// because the parent is a [StatelessWidget]) then to use [SizedBox.shrink]
/// instead of the child that would otherwise be included.
///
/// See also:
///
/// * [AnimatedSwitcher], which can fade from one child to the next as the
/// subtree changes.
/// * [AnimatedCrossFade], which can fade between two specific children.
class Visibility extends StatelessWidget {
/// Control whether the given [child] is [visible].
///
/// The [child] and [replacement] arguments must not be null.
///
/// The boolean arguments must not be null.
///
/// The [maintainSemantics] and [maintainInteractivity] arguments can only be
/// set if [maintainSize] is set.
///
/// The [maintainSize] argument can only be set if [maintainAnimation] is set.
///
/// The [maintainAnimation] argument can only be set if [maintainState] is
/// set.
const Visibility({
Key? key,
required this.child,
this.replacement = const SizedBox.shrink(),
this.visible = true,
this.maintainState = false,
this.maintainAnimation = false,
this.maintainSize = false,
this.maintainSemantics = false,
this.maintainInteractivity = false,
}) : assert(child != null),
assert(replacement != null),
assert(visible != null),
assert(maintainState != null),
assert(maintainAnimation != null),
assert(maintainSize != null),
assert(
maintainState == true || maintainAnimation == false,
'Cannot maintain animations if the state is not also maintained.',
),
assert(
maintainAnimation == true || maintainSize == false,
'Cannot maintain size if animations are not maintained.',
),
assert(
maintainSize == true || maintainSemantics == false,
'Cannot maintain semantics if size is not maintained.',
),
assert(
maintainSize == true || maintainInteractivity == false,
'Cannot maintain interactivity if size is not maintained.',
),
super(key: key);
/// The widget to show or hide, as controlled by [visible].
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
/// The widget to use when the child is not [visible], assuming that none of
/// the `maintain` flags (in particular, [maintainState]) are set.
///
/// The normal behavior is to replace the widget with a zero by zero box
/// ([SizedBox.shrink]).
///
/// See also:
///
/// * [AnimatedCrossFade], which can animate between two children.
final Widget replacement;
/// Switches between showing the [child] or hiding it.
///
/// The `maintain` flags should be set to the same values regardless of the
/// state of the [visible] property, otherwise they will not operate correctly
/// (specifically, the state will be lost regardless of the state of
/// [maintainState] whenever any of the `maintain` flags are changed, since
/// doing so will result in a subtree shape change).
///
/// Unless [maintainState] is set, the [child] subtree will be disposed
/// (removed from the tree) while hidden.
final bool visible;
/// Whether to maintain the [State] objects of the [child] subtree when it is
/// not [visible].
///
/// Keeping the state of the subtree is potentially expensive (because it
/// means all the objects are still in memory; their resources are not
/// released). It should only be maintained if it cannot be recreated on
/// demand. One example of when the state would be maintained is if the child
/// subtree contains a [Navigator], since that widget maintains elaborate
/// state that cannot be recreated on the fly.
///
/// If this property is true, an [Offstage] widget is used to hide the child
/// instead of replacing it with [replacement].
///
/// If this property is false, then [maintainAnimation] must also be false.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainState;
/// Whether to maintain animations within the [child] subtree when it is
/// not [visible].
///
/// To set this, [maintainState] must also be set.
///
/// Keeping animations active when the widget is not visible is even more
/// expensive than only maintaining the state.
///
/// One example when this might be useful is if the subtree is animating its
/// layout in time with an [AnimationController], and the result of that
/// layout is being used to influence some other logic. If this flag is false,
/// then any [AnimationController]s hosted inside the [child] subtree will be
/// muted while the [visible] flag is false.
///
/// If this property is true, no [TickerMode] widget is used.
///
/// If this property is false, then [maintainSize] must also be false.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainAnimation;
/// Whether to maintain space for where the widget would have been.
///
/// To set this, [maintainAnimation] and [maintainState] must also be set.
///
/// Maintaining the size when the widget is not [visible] is not notably more
/// expensive than just keeping animations running without maintaining the
/// size, and may in some circumstances be slightly cheaper if the subtree is
/// simple and the [visible] property is frequently toggled, since it avoids
/// triggering a layout change when the [visible] property is toggled. If the
/// [child] subtree is not trivial then it is significantly cheaper to not
/// even keep the state (see [maintainState]).
///
/// If this property is true, [Opacity] is used instead of [Offstage].
///
/// If this property is false, then [maintainSemantics] and
/// [maintainInteractivity] must also be false.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
///
/// See also:
///
/// * [AnimatedOpacity] and [FadeTransition], which apply animations to the
/// opacity for a more subtle effect.
final bool maintainSize;
/// Whether to maintain the semantics for the widget when it is hidden (e.g.
/// for accessibility).
///
/// To set this, [maintainSize] must also be set.
///
/// By default, with [maintainSemantics] set to false, the [child] is not
/// visible to accessibility tools when it is hidden from the user. If this
/// flag is set to true, then accessibility tools will report the widget as if
/// it was present.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainSemantics;
/// Whether to allow the widget to be interactive when hidden.
///
/// To set this, [maintainSize] must also be set.
///
/// By default, with [maintainInteractivity] set to false, touch events cannot
/// reach the [child] when it is hidden from the user. If this flag is set to
/// true, then touch events will nonetheless be passed through.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainInteractivity;
@override
Widget build(BuildContext context) {
if (maintainSize) {
Widget result = child;
if (!maintainInteractivity) {
result = IgnorePointer(
child: child,
ignoring: !visible,
ignoringSemantics: !visible && !maintainSemantics,
);
}
return Opacity(
opacity: visible ? 1.0 : 0.0,
alwaysIncludeSemantics: maintainSemantics,
child: result,
);
}
assert(!maintainInteractivity);
assert(!maintainSemantics);
assert(!maintainSize);
if (maintainState) {
Widget result = child;
if (!maintainAnimation)
result = TickerMode(child: child, enabled: visible);
return Offstage(
child: result,
offstage: !visible,
);
}
assert(!maintainAnimation);
assert(!maintainState);
return visible ? child : replacement;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(FlagProperty('visible', value: visible, ifFalse: 'hidden', ifTrue: 'visible'));
properties.add(FlagProperty('maintainState', value: maintainState, ifFalse: 'maintainState'));
properties.add(FlagProperty('maintainAnimation', value: maintainAnimation, ifFalse: 'maintainAnimation'));
properties.add(FlagProperty('maintainSize', value: maintainSize, ifFalse: 'maintainSize'));
properties.add(FlagProperty('maintainSemantics', value: maintainSemantics, ifFalse: 'maintainSemantics'));
properties.add(FlagProperty('maintainInteractivity', value: maintainInteractivity, ifFalse: 'maintainInteractivity'));
}
}
/// Whether to show or hide a sliver child.
///
/// By default, the [visible] property controls whether the [sliver] is included
/// in the subtree or not; when it is not [visible], the [replacementSliver] is
/// included instead.
///
/// A variety of flags can be used to tweak exactly how the sliver is hidden.
/// (Changing the flags dynamically is discouraged, as it can cause the [sliver]
/// subtree to be rebuilt, with any state in the subtree being discarded.
/// Typically, only the [visible] flag is changed dynamically.)
///
/// These widgets provide some of the facets of this one:
///
/// * [SliverOpacity], which can stop its sliver child from being painted.
/// * [SliverOffstage], which can stop its sliver child from being laid out or
/// painted.
/// * [TickerMode], which can stop its child from being animated.
/// * [ExcludeSemantics], which can hide the child from accessibility tools.
/// * [SliverIgnorePointer], which can disable touch interactions with the
/// sliver child.
///
/// Using this widget is not necessary to hide children. The simplest way to
/// hide a child is just to not include it, or, if a child _must_ be given (e.g.
/// because the parent is a [StatelessWidget]) then to use a childless
/// [SliverToBoxAdapter] instead of the child that would otherwise be included.
class SliverVisibility extends StatelessWidget {
/// Control whether the given [sliver] is [visible].
///
/// The [sliver] and [replacementSliver] arguments must not be null.
///
/// The boolean arguments must not be null.
///
/// The [maintainSemantics] and [maintainInteractivity] arguments can only be
/// set if [maintainSize] is set.
///
/// The [maintainSize] argument can only be set if [maintainAnimation] is set.
///
/// The [maintainAnimation] argument can only be set if [maintainState] is
/// set.
const SliverVisibility({
Key? key,
required this.sliver,
this.replacementSliver = const SliverToBoxAdapter(),
this.visible = true,
this.maintainState = false,
this.maintainAnimation = false,
this.maintainSize = false,
this.maintainSemantics = false,
this.maintainInteractivity = false,
}) : assert(sliver != null),
assert(replacementSliver != null),
assert(visible != null),
assert(maintainState != null),
assert(maintainAnimation != null),
assert(maintainSize != null),
assert(maintainSemantics != null),
assert(maintainInteractivity != null),
assert(
maintainState == true || maintainAnimation == false,
'Cannot maintain animations if the state is not also maintained.',
),
assert(
maintainAnimation == true || maintainSize == false,
'Cannot maintain size if animations are not maintained.',
),
assert(
maintainSize == true || maintainSemantics == false,
'Cannot maintain semantics if size is not maintained.',
),
assert(
maintainSize == true || maintainInteractivity == false,
'Cannot maintain interactivity if size is not maintained.',
),
super(key: key);
/// The sliver to show or hide, as controlled by [visible].
final Widget sliver;
/// The widget to use when the sliver child is not [visible], assuming that
/// none of the `maintain` flags (in particular, [maintainState]) are set.
///
/// The normal behavior is to replace the widget with a childless
/// [SliverToBoxAdapter], which by default has a geometry of
/// [SliverGeometry.zero].
final Widget replacementSliver;
/// Switches between showing the [sliver] or hiding it.
///
/// The `maintain` flags should be set to the same values regardless of the
/// state of the [visible] property, otherwise they will not operate correctly
/// (specifically, the state will be lost regardless of the state of
/// [maintainState] whenever any of the `maintain` flags are changed, since
/// doing so will result in a subtree shape change).
///
/// Unless [maintainState] is set, the [sliver] subtree will be disposed
/// (removed from the tree) while hidden.
final bool visible;
/// Whether to maintain the [State] objects of the [sliver] subtree when it is
/// not [visible].
///
/// Keeping the state of the subtree is potentially expensive (because it
/// means all the objects are still in memory; their resources are not
/// released). It should only be maintained if it cannot be recreated on
/// demand. One example of when the state would be maintained is if the sliver
/// subtree contains a [Navigator], since that widget maintains elaborate
/// state that cannot be recreated on the fly.
///
/// If this property is true, a [SliverOffstage] widget is used to hide the
/// sliver instead of replacing it with [replacementSliver].
///
/// If this property is false, then [maintainAnimation] must also be false.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainState;
/// Whether to maintain animations within the [sliver] subtree when it is
/// not [visible].
///
/// To set this, [maintainState] must also be set.
///
/// Keeping animations active when the widget is not visible is even more
/// expensive than only maintaining the state.
///
/// One example when this might be useful is if the subtree is animating its
/// layout in time with an [AnimationController], and the result of that
/// layout is being used to influence some other logic. If this flag is false,
/// then any [AnimationController]s hosted inside the [sliver] subtree will be
/// muted while the [visible] flag is false.
///
/// If this property is true, no [TickerMode] widget is used.
///
/// If this property is false, then [maintainSize] must also be false.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainAnimation;
/// Whether to maintain space for where the sliver would have been.
///
/// To set this, [maintainAnimation] must also be set.
///
/// Maintaining the size when the sliver is not [visible] is not notably more
/// expensive than just keeping animations running without maintaining the
/// size, and may in some circumstances be slightly cheaper if the subtree is
/// simple and the [visible] property is frequently toggled, since it avoids
/// triggering a layout change when the [visible] property is toggled. If the
/// [sliver] subtree is not trivial then it is significantly cheaper to not
/// even keep the state (see [maintainState]).
///
/// If this property is true, [SliverOpacity] is used instead of
/// [SliverOffstage].
///
/// If this property is false, then [maintainSemantics] and
/// [maintainInteractivity] must also be false.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainSize;
/// Whether to maintain the semantics for the sliver when it is hidden (e.g.
/// for accessibility).
///
/// To set this, [maintainSize] must also be set.
///
/// By default, with [maintainSemantics] set to false, the [sliver] is not
/// visible to accessibility tools when it is hidden from the user. If this
/// flag is set to true, then accessibility tools will report the widget as if
/// it was present.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainSemantics;
/// Whether to allow the sliver to be interactive when hidden.
///
/// To set this, [maintainSize] must also be set.
///
/// By default, with [maintainInteractivity] set to false, touch events cannot
/// reach the [sliver] when it is hidden from the user. If this flag is set to
/// true, then touch events will nonetheless be passed through.
///
/// Dynamically changing this value may cause the current state of the
/// subtree to be lost (and a new instance of the subtree, with new [State]
/// objects, to be immediately created if [visible] is true).
final bool maintainInteractivity;
@override
Widget build(BuildContext context) {
if (maintainSize) {
Widget result = sliver;
if (!maintainInteractivity) {
result = SliverIgnorePointer(
sliver: sliver,
ignoring: !visible,
ignoringSemantics: !visible && !maintainSemantics,
);
}
return SliverOpacity(
opacity: visible ? 1.0 : 0.0,
alwaysIncludeSemantics: maintainSemantics,
sliver: result,
);
}
assert(!maintainInteractivity);
assert(!maintainSemantics);
assert(!maintainSize);
if (maintainState) {
Widget result = sliver;
if (!maintainAnimation)
result = TickerMode(child: sliver, enabled: visible);
return SliverOffstage(
sliver: result,
offstage: !visible,
);
}
assert(!maintainAnimation);
assert(!maintainState);
return visible ? sliver : replacementSliver;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(FlagProperty('visible', value: visible, ifFalse: 'hidden', ifTrue: 'visible'));
properties.add(FlagProperty('maintainState', value: maintainState, ifFalse: 'maintainState'));
properties.add(FlagProperty('maintainAnimation', value: maintainAnimation, ifFalse: 'maintainAnimation'));
properties.add(FlagProperty('maintainSize', value: maintainSize, ifFalse: 'maintainSize'));
properties.add(FlagProperty('maintainSemantics', value: maintainSemantics, ifFalse: 'maintainSemantics'));
properties.add(FlagProperty('maintainInteractivity', value: maintainInteractivity, ifFalse: 'maintainInteractivity'));
}
}