InheritedWidget class

Base class for widgets that efficiently propagate information down the tree.

To obtain the nearest instance of a particular type of inherited widget from a build context, use BuildContext.inheritFromWidgetOfExactType.

Inherited widgets, when referenced in this way, will cause the consumer to rebuild when the inherited widget itself changes state.

The following is a skeleton of an inherited widget called FrogColor:
class FrogColor extends InheritedWidget {
  const FrogColor({
    Key key,
    @required this.color,
    @required Widget child,
  }) : assert(color != null),
       assert(child != null),
       super(key: key, child: child);

  final Color color;

  static FrogColor of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(FrogColor);
  }

  @override
  bool updateShouldNotify(FrogColor old) => color != old.color;
}

The convention is to provide a static method of on the InheritedWidget which does the call to BuildContext.inheritFromWidgetOfExactType. This allows the class to define its own fallback logic in the case of there not being a widget in scope. In the example above, the value returned will be null in that case, but it could also have defaulted to a value.

Sometimes, the of method returns the data rather than the inherited widget; for example, in this case it could have returned a Color instead of the FrogColor widget.

Occasionally, the inherited widget is an implementation detail of another class, and is therefore private. The of method in that case is typically put on the public class instead. For example, Theme is implemented as a StatelessWidget that builds a private inherited widget; Theme.of looks for that inherited widget using BuildContext.inheritFromWidgetOfExactType and then returns the ThemeData.

See also:

  • StatefulWidget and State, for widgets that can build differently several times over their lifetime.
  • StatelessWidget, for widgets that always build the same way given a particular configuration and ambient state.
  • Widget, for an overview of widgets in general.
  • InheritedNotifier, an inherited widget whose value can be a Listenable, and which will notify dependents whenever the value sends notifications.
  • InheritedModel, an inherited widget that allows clients to subscribe to changes for subparts of the value.
Inheritance
Implementers

Constructors

InheritedWidget({Key key, Widget child })
Abstract const constructor. This constructor enables subclasses to provide const constructors so that they can be used in const expressions.
const

Properties

child Widget
The widget below this widget in the tree. [...]
final, inherited
hashCode int
The hash code for this object. [...]
read-only, inherited
key Key
Controls how one widget replaces another widget in the tree. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

createElement() InheritedElement
Inflates this configuration to a concrete instance. [...]
override
updateShouldNotify(covariant InheritedWidget oldWidget) bool
Whether the framework should notify widgets that inherit from this widget. [...]
@protected
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children. [...]
@protected, inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node. [...]
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toDiagnosticsNode({String name, DiagnosticsTreeStyle style }) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by toStringDeep. [...]
inherited
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug }) String
Returns a string representation of this object.
inherited
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug }) String
Returns a string representation of this node and its descendants. [...]
inherited
toStringShallow({String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug }) String
Returns a one-line detailed description of the object. [...]
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited