didUpdateWidget method

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

Called whenever the widget configuration changes.

If the parent widget rebuilds and requests that this location in the tree update to display a new widget with the same runtimeType and Widget.key, the framework will update the widget property of this State object to refer to the new widget and then call this method with the previous widget as an argument.

Override this method to respond when the widget changes (e.g., to start implicit animations).

The framework always calls build after calling didUpdateWidget, which means any calls to setState in didUpdateWidget are redundant.

If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:

  • In initState, subscribe to the object.
  • In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
  • In dispose, unsubscribe from the object.

Implementations of this method should start with a call to the inherited method, as in super.didUpdateWidget(oldWidget).

See the discussion at Element.rebuild for more information on when this method is called.

Implementation

@override
void didUpdateWidget(Scaffold oldWidget) {
  super.didUpdateWidget(oldWidget);
  // Update the Floating Action Button Animator, and then schedule the Floating Action Button for repositioning.
  if (widget.floatingActionButtonAnimator != oldWidget.floatingActionButtonAnimator) {
    _floatingActionButtonAnimator = widget.floatingActionButtonAnimator ?? _kDefaultFloatingActionButtonAnimator;
  }
  if (widget.floatingActionButtonLocation != oldWidget.floatingActionButtonLocation) {
    _moveFloatingActionButton(widget.floatingActionButtonLocation ?? _kDefaultFloatingActionButtonLocation);
  }
  if (widget.bottomSheet != oldWidget.bottomSheet) {
    assert(() {
      if (widget.bottomSheet != null && (_currentBottomSheet?._isLocalHistoryEntry ?? false)) {
        throw FlutterError.fromParts(<DiagnosticsNode>[
          ErrorSummary(
            'Scaffold.bottomSheet cannot be specified while a bottom sheet displayed '
            'with showBottomSheet() is still visible.',
          ),
          ErrorHint(
            'Use the PersistentBottomSheetController '
            'returned by showBottomSheet() to close the old bottom sheet before creating '
            'a Scaffold with a (non null) bottomSheet.',
          ),
        ]);
      }
      return true;
    }());
    if (widget.bottomSheet == null) {
      _closeCurrentBottomSheet();
    } else if (widget.bottomSheet != null && oldWidget.bottomSheet == null) {
      _maybeBuildPersistentBottomSheet();
    } else {
      _updatePersistentBottomSheet();
    }
  }
}