geometryOf method Null safety

ValueListenable<ScaffoldGeometry> geometryOf(
  1. BuildContext context
)

Returns a ValueListenable for the ScaffoldGeometry for the closest Scaffold ancestor of the given context.

The ValueListenable.value is only available at paint time.

Notifications are guaranteed to be sent before the first paint pass with the new geometry, but there is no guarantee whether a build or layout passes are going to happen between the notification and the next paint pass.

The closest Scaffold ancestor for the context might change, e.g when an element is moved from one scaffold to another. For StatefulWidgets using this listenable, a change of the Scaffold ancestor will trigger a State.didChangeDependencies.

A typical pattern for listening to the scaffold geometry would be to call Scaffold.geometryOf in State.didChangeDependencies, compare the return value with the previous listenable, if it has changed, unregister the listener, and register a listener to the new ScaffoldGeometry listenable.

Implementation

static ValueListenable<ScaffoldGeometry> geometryOf(BuildContext context) {
  final _ScaffoldScope? scaffoldScope = context.dependOnInheritedWidgetOfExactType<_ScaffoldScope>();
  if (scaffoldScope == null) {
    throw FlutterError.fromParts(<DiagnosticsNode>[
      ErrorSummary(
        'Scaffold.geometryOf() called with a context that does not contain a Scaffold.',
      ),
      ErrorDescription(
        'This usually happens when the context provided is from the same StatefulWidget as that '
        'whose build function actually creates the Scaffold widget being sought.',
      ),
      ErrorHint(
        'There are several ways to avoid this problem. The simplest is to use a Builder to get a '
        'context that is "under" the Scaffold. For an example of this, please see the '
        'documentation for Scaffold.of():\n'
        '  https://api.flutter.dev/flutter/material/Scaffold/of.html',
      ),
      ErrorHint(
        'A more efficient solution is to split your build function into several widgets. This '
        'introduces a new context from which you can obtain the Scaffold. In this solution, '
        'you would have an outer widget that creates the Scaffold populated by instances of '
        'your new inner widgets, and then in these inner widgets you would use Scaffold.geometryOf().',
      ),
      context.describeElement('The context used was'),
    ]);
  }
  return scaffoldScope.geometryNotifier;
}