InheritedWidget class Null safety
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.dependOnInheritedWidgetOfExactType.
Inherited widgets, when referenced in this way, will cause the consumer to rebuild when the inherited widget itself changes state.
FrogColor
:
class FrogColor extends InheritedWidget {
const FrogColor({
super.key,
required this.color,
required super.child,
});
final Color color;
static FrogColor of(BuildContext context) {
final FrogColor? result = context.dependOnInheritedWidgetOfExactType<FrogColor>();
assert(result != null, 'No FrogColor found in context');
return result!;
}
@override
bool updateShouldNotify(FrogColor old) => color != old.color;
}
Implementing the of
method
The convention is to provide a static method of
on the InheritedWidget
which does the call to BuildContext.dependOnInheritedWidgetOfExactType. This
allows the class to define its own fallback logic in case there isn't
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.dependOnInheritedWidgetOfExactType
and then returns the ThemeData.
Calling the of
method
When using the of
method, the context
must be a descendant of the
InheritedWidget, meaning it must be "below" the InheritedWidget in the
tree.
context
used is the one from the Builder, which is
a child of the FrogColor widget, so this works.
class MyPage extends StatelessWidget {
const MyPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: FrogColor(
color: Colors.green,
child: Builder(
builder: (BuildContext innerContext) {
return Text(
'Hello Frog',
style: TextStyle(color: FrogColor.of(innerContext).color),
);
},
),
),
);
}
}
context
used is the one from the MyOtherPage widget,
which is a parent of the FrogColor widget, so this does not work.
class MyOtherPage extends StatelessWidget {
const MyOtherPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: FrogColor(
color: Colors.green,
child: Text(
'Hello Frog',
style: TextStyle(color: FrogColor.of(context).color),
),
),
);
}
}
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
-
- Object
- DiagnosticableTree
- Widget
- ProxyWidget
- InheritedWidget
- Implementers
- BottomNavigationBarTheme
- ButtonBarTheme
- CheckboxTheme
- CupertinoUserInterfaceLevel
- DataTableTheme
- DefaultAssetBundle
- Directionality
- DropdownButtonHideUnderline
- FlexibleSpaceBarSettings
- FocusTraversalOrder
- HeroControllerScope
- InheritedModel
- InheritedNotifier
- InheritedTheme
- MediaQuery
- PrimaryScrollController
- RadioTheme
- ScrollbarTheme
- ScrollConfiguration
- SelectionRegistrarScope
- SwitchTheme
- UnmanagedRestorationScope
Constructors
- InheritedWidget({Key? key, required Widget child})
-
Abstract const constructor. This constructor enables subclasses to provide
const constructors so that they can be used in const expressions.
const
Properties
Methods
-
createElement(
) → InheritedElement -
Inflates this configuration to a concrete instance.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of
DiagnosticsNode
objects describing this node's children.protected">@protectedinherited -
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 DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
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
-
updateShouldNotify(
covariant InheritedWidget oldWidget) → bool -
Whether the framework should notify widgets that inherit from this widget.
protected">@protected
Operators
-
operator ==(
Object other) → bool -
The equality operator.
nonVirtual">@nonVirtualinherited