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.

Sample code

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);

  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.
Implemented by


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.


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


createElement() InheritedElement
Inflates this configuration to a concrete instance. [...]
updateShouldNotify(InheritedWidget oldWidget) → bool
Whether the framework should notify widgets that inherit from this widget. [...]
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. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toDiagnosticsNode({String name, DiagnosticsTreeStyle style }) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by toStringDeep. [...]
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String
Returns a string representation of this object.
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String
Returns a string representation of this node and its descendants. [...]
toStringShallow({String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug }) → String
Returns a one-line detailed description of the object. [...]
toStringShort() → String
A short, textual description of this widget.


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