didChangeDependencies method

  1. @override
void didChangeDependencies()
override

Called when a dependency of this State object changes.

For example, if the previous call to build referenced an InheritedWidget that later changed, the framework would call this method to notify this object about the change.

This method is also called immediately after initState. It is safe to call BuildContext.dependOnInheritedWidgetOfExactType from this method.

Subclasses rarely override this method because the framework always calls build after a dependency changes. Some subclasses do override this method because they need to do some expensive work (e.g., network fetches) when their dependencies change, and that work would be too expensive to do for every build.

Implementation

@override
void didChangeDependencies() {
  super.didChangeDependencies();

  _style = MediaQuery.boldTextOf(context)
      ? widget.style.merge(const TextStyle(fontWeight: FontWeight.bold))
      : widget.style;

  final AutofillGroupState? newAutofillGroup = AutofillGroup.maybeOf(context);
  if (currentAutofillScope != newAutofillGroup) {
    _currentAutofillScope?.unregister(autofillId);
    _currentAutofillScope = newAutofillGroup;
    _currentAutofillScope?.register(_effectiveAutofillClient);
  }

  if (!_didAutoFocus && widget.autofocus) {
    _didAutoFocus = true;
    SchedulerBinding.instance.addPostFrameCallback((_) {
      if (mounted && renderEditable.hasSize) {
        _flagInternalFocus();
        FocusScope.of(context).autofocus(widget.focusNode);
      }
    }, debugLabel: 'EditableText.autofocus');
  }

  // Restart or stop the blinking cursor when TickerMode changes.
  final bool newTickerEnabled = TickerMode.of(context);
  if (_tickersEnabled != newTickerEnabled) {
    _tickersEnabled = newTickerEnabled;
    if (_showBlinkingCursor) {
      _startCursorBlink();
    } else if (!_tickersEnabled && _cursorTimer != null) {
      _stopCursorBlink();
    }
  }

  // Check for changes in viewId.
  if (_hasInputConnection) {
    final int newViewId = View.of(context).viewId;
    if (newViewId != _viewId) {
      _textInputConnection!.updateConfig(_effectiveAutofillClient.textInputConfiguration);
    }
  }

  if (defaultTargetPlatform != TargetPlatform.iOS && defaultTargetPlatform != TargetPlatform.android) {
    return;
  }

  // Hide the text selection toolbar on mobile when orientation changes.
  final Orientation orientation = MediaQuery.orientationOf(context);
  if (_lastOrientation == null) {
    _lastOrientation = orientation;
    return;
  }
  if (orientation != _lastOrientation) {
    _lastOrientation = orientation;
    if (defaultTargetPlatform == TargetPlatform.iOS) {
      hideToolbar(false);
    }
    if (defaultTargetPlatform == TargetPlatform.android) {
      hideToolbar();
    }
  }

  if (_listeningToScrollNotificationObserver) {
    // Only update subscription when we have previously subscribed to the
    // scroll notification observer. We only subscribe to the scroll
    // notification observer when the context menu is shown on platforms that
    // support _platformSupportsFadeOnScroll.
    _scrollNotificationObserver?.removeListener(_handleContextMenuOnParentScroll);
    _scrollNotificationObserver = ScrollNotificationObserver.maybeOf(context);
    _scrollNotificationObserver?.addListener(_handleContextMenuOnParentScroll);
  }
}