@@ -517,8 +517,21 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
517
517
/// focus traversal policy for a widget subtree.
518
518
/// * [FocusTraversalPolicy] , a class that can be extended to describe a
519
519
/// traversal policy.
520
- bool get canRequestFocus => _canRequestFocus && ancestors.every (_allowDescendantsToBeFocused);
521
- static bool _allowDescendantsToBeFocused (FocusNode ancestor) => ancestor.descendantsAreFocusable;
520
+ bool get canRequestFocus {
521
+ if (! _canRequestFocus) {
522
+ return false ;
523
+ }
524
+ final FocusScopeNode ? scope = enclosingScope;
525
+ if (scope != null && ! scope.canRequestFocus) {
526
+ return false ;
527
+ }
528
+ for (final FocusNode ancestor in ancestors) {
529
+ if (! ancestor.descendantsAreFocusable) {
530
+ return false ;
531
+ }
532
+ }
533
+ return true ;
534
+ }
522
535
523
536
bool _canRequestFocus;
524
537
@mustCallSuper
@@ -778,22 +791,6 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
778
791
/// Use [enclosingScope] to look for scopes above this node.
779
792
FocusScopeNode ? get nearestScope => enclosingScope;
780
793
781
- FocusScopeNode ? _enclosingScope;
782
- void _clearEnclosingScopeCache () {
783
- final FocusScopeNode ? cachedScope = _enclosingScope;
784
- if (cachedScope == null ) {
785
- return ;
786
- }
787
- _enclosingScope = null ;
788
- if (children.isNotEmpty) {
789
- for (final FocusNode child in children) {
790
- if (identical (cachedScope, child._enclosingScope)) {
791
- child._clearEnclosingScopeCache ();
792
- }
793
- }
794
- }
795
- }
796
-
797
794
/// Returns the nearest enclosing scope node above this node, or null if the
798
795
/// node has not yet be added to the focus tree.
799
796
///
@@ -802,9 +799,12 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
802
799
///
803
800
/// Use [nearestScope] to start at this node instead of above it.
804
801
FocusScopeNode ? get enclosingScope {
805
- final FocusScopeNode ? enclosingScope = _enclosingScope ?? = parent? .nearestScope;
806
- assert (enclosingScope == parent? .nearestScope, '$this has invalid scope cache: $_enclosingScope != ${parent ?.nearestScope }' );
807
- return enclosingScope;
802
+ for (final FocusNode node in ancestors) {
803
+ if (node is FocusScopeNode ) {
804
+ return node;
805
+ }
806
+ }
807
+ return null ;
808
808
}
809
809
810
810
/// Returns the size of the attached widget's [RenderObject] , in logical
@@ -990,7 +990,6 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
990
990
}
991
991
992
992
node._parent = null ;
993
- node._clearEnclosingScopeCache ();
994
993
_children.remove (node);
995
994
for (final FocusNode ancestor in ancestors) {
996
995
ancestor._descendants = null ;
@@ -1269,14 +1268,13 @@ class FocusScopeNode extends FocusNode {
1269
1268
super .skipTraversal,
1270
1269
super .canRequestFocus,
1271
1270
this .traversalEdgeBehavior = TraversalEdgeBehavior .closedLoop,
1272
- }) : super (descendantsAreFocusable: true );
1271
+ }) : super (
1272
+ descendantsAreFocusable: true ,
1273
+ );
1273
1274
1274
1275
@override
1275
1276
FocusScopeNode get nearestScope => this ;
1276
1277
1277
- @override
1278
- bool get descendantsAreFocusable => _canRequestFocus && super .descendantsAreFocusable;
1279
-
1280
1278
/// Controls the transfer of focus beyond the first and the last items of a
1281
1279
/// [FocusScopeNode] .
1282
1280
///
0 commit comments