Sub topic 15
InheritedWidget and Dependency Tracking
This chapter assumes basic Dart, StatelessWidget and StatefulWidget, and common layout widgets such as Column, Padding, and Text.
Why inherited data exists
A widget often needs data from an ancestor. Passing that data through every constructor works for small trees, but becomes noisy when many descendants need the same value. InheritedWidget provides another path: an ancestor publishes a value, and descendants subscribe to it through their BuildContext.
BuildContext identifies an element in the element tree. When a descendant calls dependOnInheritedWidgetOfExactType, Flutter records that element as a dependent of the nearest matching InheritedElement.
import 'package:flutter/widgets.dart';
void main() => runApp(const Demo());
class AccentScope extends InheritedWidget {
const AccentScope({
super.key,
required this.color,
required super.child,
});
final Color color;
static AccentScope of(BuildContext context) {
final scope =
context.dependOnInheritedWidgetOfExactType<AccentScope>();
assert(scope != null, 'AccentScope is missing above this widget');
return scope!;
}
@override
bool updateShouldNotify(AccentScope oldWidget) {
return color != oldWidget.color;
}
}
class Badge extends StatelessWidget {
const Badge({super.key});
@override
Widget build(BuildContext context) {
final accent = AccentScope.of(context);
return Text(
'The current color is ${accent.color}',
style: TextStyle(color: accent.color),
);
}
}
class Demo extends StatefulWidget {
const Demo({super.key});
@override
State<Demo> createState() => _DemoState();
}
class _DemoState extends State<Demo> {
bool _useBlue = true;
@override
Widget build(BuildContext context) {
final color = _useBlue
? const Color(0xff1565c0)
: const Color(0xffc62828);
return Directionality(
textDirection: TextDirection.ltr,
child: AccentScope(
color: color,
child: Column(
children: [
const Badge(),
GestureDetector(
onTap: () => setState(() => _useBlue = !_useBlue),
child: const Text('Change color'),
),
],
),
),
);
}
}updateShouldNotify compares the new inherited widget with the old one. If it returns true, registered dependents rebuild. Their State objects and elements are normally reused.
Dependency tracking and lifecycle
A dependent usually registers during build. Flutter also calls State.didChangeDependencies after the widget first obtains a dependency and whenever that dependency changes. This method is useful for work that depends on inherited data, such as starting a subscription based on a locale or media setting.
Use dependOnInheritedWidgetOfExactType when a later change should rebuild the caller. Use getInheritedWidgetOfExactType when you only need to read the current value and do not want to register a dependency.
What happens during an update
When Demo calls setState, its element becomes dirty. Flutter rebuilds it and creates a new AccentScope configuration. The existing InheritedElement compares the old and new values. If the color changed, it notifies Badge's element, which rebuilds even though Badge itself is const.
Exercise
What is the difference between dependOnInheritedWidgetOfExactType and getInheritedWidgetOfExactType?
Show answer
The first registers a dependency and rebuilds the caller when the inherited value changes. The second reads the value without registering that dependency.