of static method

Finds the TransparentScaffoldState from the closest instance of this class that encloses the given context.

If no instance of this class encloses the given context, will cause an assert in debug mode, and throw an exception in release mode.

This method can be expensive (it walks the element tree).

{@tool dartpad} Typical usage of the TransparentScaffold.of function is to call it from within the build method of a child of a TransparentScaffold.

** See code in examples/api/lib/material/scaffold/scaffold.of.0.dart ** {@end-tool}

{@tool dartpad} When the TransparentScaffold is actually created in the same build function, the context argument to the build function can't be used to find the TransparentScaffold (since it's "above" the widget being returned in the widget tree). In such cases, the following technique with a Builder can be used to provide a new scope with a BuildContext that is "under" the Scaffold:

** See code in examples/api/lib/material/scaffold/scaffold.of.1.dart ** {@end-tool}

A more efficient solution is to split your build function into several widgets. This introduces a new context from which you can obtain the TransparentScaffold. In this solution, you would have an outer widget that creates the TransparentScaffold populated by instances of your new inner widgets, and then in these inner widgets you would use TransparentScaffold.of.

A less elegant but more expedient solution is assign a GlobalKey to the TransparentScaffold, then use the key.currentState property to obtain the TransparentScaffoldState rather than using the TransparentScaffold.of function.

If there is no TransparentScaffold in scope, then this will throw an exception. To return null if there is no TransparentScaffold, use maybeOf instead.

Implementation

static TransparentScaffoldState of(BuildContext context) {
  final TransparentScaffoldState? result =
      context.findAncestorStateOfType<TransparentScaffoldState>();
  if (result != null) {
    return result;
  }
  throw FlutterError.fromParts(<DiagnosticsNode>[
    ErrorSummary(
      'Scaffold.of() called with a context that does not contain a Scaffold.',
    ),
    ErrorDescription(
      'No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). '
      '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.of().\n'
      'A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, '
      'then use the key.currentState property to obtain the ScaffoldState rather than '
      'using the Scaffold.of() function.',
    ),
    context.describeElement('The context used was'),
  ]);
}