didUpdateWidget method

  1. @override
void didUpdateWidget(
  1. covariant Focus oldWidget
)
override

Called whenever the widget configuration changes.

Implementation

@override
void didUpdateWidget(Focus oldWidget) {
  super.didUpdateWidget(oldWidget);
  if (widget.focusNode != oldWidget.focusNode) {
    final hadPrimaryFocus = FocusManager.instance.primaryFocus == _focusNode;
    if (oldWidget.focusNode == null) {
      _focusNode.dispose();
    } else {
      final p = _focusNode.parent;
      if (p != null) {
        // Clean up parenting relationship via removeChild instead of directly
        // mutating the parent's children list, which would bypass state propagation
        // and leave the parent node in an inconsistent state if this child had focus.
        p.removeChild(_focusNode);
      } else if (_focusNode.hasFocus) {
        _focusNode.unfocus();
      }
    }
    _focusNode = widget.focusNode ?? createFocusNode();
    _updateNodeProperties();
    final parentScope = Focus.of(context);
    if (parentScope != null) {
      parentScope.addChild(_focusNode);
    }
    if (hadPrimaryFocus) {
      _focusNode.requestFocus();
    }
  } else {
    _updateNodeProperties();
  }
}