The [internal test failure](https://github.com/flutter/flutter/pull/144207#issuecomment-1968236339) was caused by `Focus.withExternalFocusNode` modifying the external node's attributes. The extra changes are in this commit: e53d98b06c
CL with (almost) passing TGP: cl/611157582
This commit is contained in:
@@ -517,21 +517,8 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
|
||||
/// focus traversal policy for a widget subtree.
|
||||
/// * [FocusTraversalPolicy], a class that can be extended to describe a
|
||||
/// traversal policy.
|
||||
bool get canRequestFocus {
|
||||
if (!_canRequestFocus) {
|
||||
return false;
|
||||
}
|
||||
final FocusScopeNode? scope = enclosingScope;
|
||||
if (scope != null && !scope.canRequestFocus) {
|
||||
return false;
|
||||
}
|
||||
for (final FocusNode ancestor in ancestors) {
|
||||
if (!ancestor.descendantsAreFocusable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool get canRequestFocus => _canRequestFocus && ancestors.every(_allowDescendantsToBeFocused);
|
||||
static bool _allowDescendantsToBeFocused(FocusNode ancestor) => ancestor.descendantsAreFocusable;
|
||||
|
||||
bool _canRequestFocus;
|
||||
@mustCallSuper
|
||||
@@ -791,6 +778,22 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
|
||||
/// Use [enclosingScope] to look for scopes above this node.
|
||||
FocusScopeNode? get nearestScope => enclosingScope;
|
||||
|
||||
FocusScopeNode? _enclosingScope;
|
||||
void _clearEnclosingScopeCache() {
|
||||
final FocusScopeNode? cachedScope = _enclosingScope;
|
||||
if (cachedScope == null) {
|
||||
return;
|
||||
}
|
||||
_enclosingScope = null;
|
||||
if (children.isNotEmpty) {
|
||||
for (final FocusNode child in children) {
|
||||
if (identical(cachedScope, child._enclosingScope)) {
|
||||
child._clearEnclosingScopeCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the nearest enclosing scope node above this node, or null if the
|
||||
/// node has not yet be added to the focus tree.
|
||||
///
|
||||
@@ -799,12 +802,9 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
|
||||
///
|
||||
/// Use [nearestScope] to start at this node instead of above it.
|
||||
FocusScopeNode? get enclosingScope {
|
||||
for (final FocusNode node in ancestors) {
|
||||
if (node is FocusScopeNode) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
final FocusScopeNode? enclosingScope = _enclosingScope ??= parent?.nearestScope;
|
||||
assert(enclosingScope == parent?.nearestScope, '$this has invalid scope cache: $_enclosingScope != ${parent?.nearestScope}');
|
||||
return enclosingScope;
|
||||
}
|
||||
|
||||
/// Returns the size of the attached widget's [RenderObject], in logical
|
||||
@@ -990,6 +990,7 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
|
||||
}
|
||||
|
||||
node._parent = null;
|
||||
node._clearEnclosingScopeCache();
|
||||
_children.remove(node);
|
||||
for (final FocusNode ancestor in ancestors) {
|
||||
ancestor._descendants = null;
|
||||
@@ -1268,13 +1269,14 @@ class FocusScopeNode extends FocusNode {
|
||||
super.skipTraversal,
|
||||
super.canRequestFocus,
|
||||
this.traversalEdgeBehavior = TraversalEdgeBehavior.closedLoop,
|
||||
}) : super(
|
||||
descendantsAreFocusable: true,
|
||||
);
|
||||
}) : super(descendantsAreFocusable: true);
|
||||
|
||||
@override
|
||||
FocusScopeNode get nearestScope => this;
|
||||
|
||||
@override
|
||||
bool get descendantsAreFocusable => _canRequestFocus && super.descendantsAreFocusable;
|
||||
|
||||
/// Controls the transfer of focus beyond the first and the last items of a
|
||||
/// [FocusScopeNode].
|
||||
///
|
||||
|
||||
@@ -511,7 +511,7 @@ class _FocusWithExternalFocusNode extends Focus {
|
||||
|
||||
class _FocusState extends State<Focus> {
|
||||
FocusNode? _internalNode;
|
||||
FocusNode get focusNode => widget.focusNode ?? _internalNode!;
|
||||
FocusNode get focusNode => widget.focusNode ?? (_internalNode ??= _createNode());
|
||||
late bool _hadPrimaryFocus;
|
||||
late bool _couldRequestFocus;
|
||||
late bool _descendantsWereFocusable;
|
||||
@@ -526,17 +526,13 @@ class _FocusState extends State<Focus> {
|
||||
}
|
||||
|
||||
void _initNode() {
|
||||
if (widget.focusNode == null) {
|
||||
// Only create a new node if the widget doesn't have one.
|
||||
// This calls a function instead of just allocating in place because
|
||||
// _createNode is overridden in _FocusScopeState.
|
||||
_internalNode ??= _createNode();
|
||||
}
|
||||
focusNode.descendantsAreFocusable = widget.descendantsAreFocusable;
|
||||
focusNode.descendantsAreTraversable = widget.descendantsAreTraversable;
|
||||
focusNode.skipTraversal = widget.skipTraversal;
|
||||
if (widget._canRequestFocus != null) {
|
||||
focusNode.canRequestFocus = widget._canRequestFocus!;
|
||||
if (!widget._usingExternalFocus) {
|
||||
focusNode.descendantsAreFocusable = widget.descendantsAreFocusable;
|
||||
focusNode.descendantsAreTraversable = widget.descendantsAreTraversable;
|
||||
focusNode.skipTraversal = widget.skipTraversal;
|
||||
if (widget._canRequestFocus != null) {
|
||||
focusNode.canRequestFocus = widget._canRequestFocus!;
|
||||
}
|
||||
}
|
||||
_couldRequestFocus = focusNode.canRequestFocus;
|
||||
_descendantsWereFocusable = focusNode.descendantsAreFocusable;
|
||||
|
||||
Reference in New Issue
Block a user