@@ -31,26 +31,56 @@ class DocumentLayoutEditable implements Editable {
31
31
void onTransactionStart () {}
32
32
}
33
33
34
+ /// Wraps document components to hide them when [hiding] is true.
35
+ ///
36
+ /// A hidden component is not visible but has its state maintained.
37
+ /// This widget will hide its child if [hiding] is true OR it has
38
+ /// an ancestor [HiddenComponent] widget with [hiding] set to true.
34
39
class HiddenComponent extends StatelessWidget {
35
- const HiddenComponent ({super .key, required this .child, this .hiding = true });
40
+ const HiddenComponent ({
41
+ super .key,
42
+ required this .hiding,
43
+ required this .child,
44
+ });
36
45
37
46
final Widget child;
38
47
final bool hiding;
39
48
40
49
@override
41
50
Widget build (BuildContext context) {
42
- return Visibility (
43
- visible: ! hiding,
44
- maintainAnimation: false ,
45
- maintainInteractivity: false ,
46
- maintainSemantics: false ,
47
- maintainSize: false ,
48
- maintainState: true ,
49
- child: child,
51
+ final computedHiding =
52
+ hiding || (context.dependOnInheritedWidgetOfExactType <_HiddenComponentScope >()? .hiding ?? false );
53
+ return _HiddenComponentScope (
54
+ hiding: computedHiding,
55
+ child: Visibility (
56
+ visible: ! computedHiding,
57
+ maintainAnimation: false ,
58
+ maintainInteractivity: false ,
59
+ maintainSemantics: false ,
60
+ maintainSize: false ,
61
+ maintainState: true ,
62
+ child: child,
63
+ ),
50
64
);
51
65
}
52
66
}
53
67
68
+ class _HiddenComponentScope extends InheritedWidget {
69
+ const _HiddenComponentScope ({required super .child, this .hiding = true });
70
+
71
+ final bool hiding;
72
+
73
+ @override
74
+ bool updateShouldNotify (_HiddenComponentScope oldWidget) {
75
+ return oldWidget.hiding != hiding;
76
+ }
77
+
78
+ static _HiddenComponentScope of (BuildContext context) => _HiddenComponentScope .maybeOf (context)! ;
79
+
80
+ static _HiddenComponentScope ? maybeOf (BuildContext context) =>
81
+ context.dependOnInheritedWidgetOfExactType <_HiddenComponentScope >();
82
+ }
83
+
54
84
/// Abstract representation of a document layout.
55
85
///
56
86
/// Regardless of how a document is displayed, a [DocumentLayout] needs
@@ -319,7 +349,7 @@ mixin DocumentComponent<T extends StatefulWidget> on State<T> {
319
349
320
350
/// Returns `true` if the component is visually hidden and should be ignored
321
351
/// for the purposes of hit testing, cursor movement, etc.
322
- bool get isHidden => context.findAncestorWidgetOfExactType < HiddenComponent >()? .hiding ?? false ;
352
+ bool get isHidden => context.dependOnInheritedWidgetOfExactType < _HiddenComponentScope >()? .hiding ?? false ;
323
353
324
354
/// Returns the desired [MouseCursor] at the given (x,y) [localOffset] , or
325
355
/// [null] if this component has no preference for the cursor style.
0 commit comments