Sub topic 9
Avoid Over-Broad Inherited Widget Dependencies
Assume you know basic Dart, StatelessWidget, StatefulWidget, setState, and the build method. This chapter focuses on inherited widgets, which provide shared data to descendants through the widget tree.
How inherited dependencies cause rebuilds
An inherited widget is a widget that makes data available to widgets below it. MediaQuery, Theme, and Localizations are common examples.
When a widget reads inherited data from BuildContext, Flutter records a dependency. If that inherited data changes, Flutter marks the dependent element dirty and calls its build method again.
The dependency can be broader than the value your widget actually uses. For example:
final size = MediaQuery.of(context).size;MediaQuery.of registers a dependency on the full MediaQueryData. The widget can rebuild when the window size changes, but also when other data changes, such as view insets, padding, text scaling, brightness, or accessibility settings.
The rebuild may be cheap, so this code is not automatically a problem. It becomes worth investigating when many widgets depend on broad data and the dependency changes frequently, such as while the keyboard opens or the device rotates.
Choose a narrow accessor
Flutter provides aspect-specific accessors for common inherited data. Use the narrowest accessor that expresses what the widget needs.
| Need | Broad lookup | Narrow lookup |
|---|---|---|
| Window size | `MediaQuery.of(context).size` | `MediaQuery.sizeOf(context)` |
| View padding | `MediaQuery.of(context).viewPadding` | `MediaQuery.viewPaddingOf(context)` |
| Text scale | `MediaQuery.of(context).textScaler` | `MediaQuery.textScalerOf(context)` |
| Platform brightness | `MediaQuery.of(context).platformBrightness` | `MediaQuery.platformBrightnessOf(context)` |
For example, a panel that only lays out from the available size can use:
class ResponsivePanel extends StatelessWidget {
const ResponsivePanel({super.key});
@override
Widget build(BuildContext context) {
final size = MediaQuery.sizeOf(context);
return SizedBox(
width: size.width * 0.8,
height: size.height * 0.4,
child: const Center(
child: Text('Responsive panel'),
),
);
}
}With MediaQuery.sizeOf, the widget depends on the size aspect rather than every field in MediaQueryData. This reduces unnecessary build calls when unrelated environmental values change.
The same principle applies to other inherited data. If a widget only needs a theme color, read that value as narrowly as the framework or state-management package allows. Avoid reading an entire model or inherited object when the widget uses one field.
Understand the dependency boundary
A dependency belongs to the widget that performs the lookup. If a parent reads MediaQuery.of(context) and passes only a color to a child, the parent is the one that rebuilds because of the broad dependency.
Move the lookup closer to the widget that needs it when practical:
flowchart TD
A[MediaQuery changes] --> B{Which aspect changed?}
B -->|Size| C[Widgets using MediaQuery.sizeOf]
B -->|Insets| D[Widgets using MediaQuery.viewInsetsOf]
B -->|Unrelated field| E[Widgets using only size are not marked]This keeps unrelated parts of a large screen out of the dependency. It also makes each widget's reason for rebuilding easier to understand.
Audit inherited lookups
Search the screen for calls such as:
MediaQuery.of(context)Theme.of(context)Localizations.of(context)context.watch<Model>()- Direct inherited-widget lookups
For each call, ask:
- Which value does this widget actually use?
- Does a narrower accessor exist?
- Does the widget need to rebuild for every change in the object?
- Would moving the lookup into a smaller widget reduce the affected subtree?
Measure before changing code. In debug mode, debugPrintRebuildDirtyWidgets can show which widgets rebuild. DevTools can help you compare frame time and rebuild behavior on a realistic device. Use profile mode for performance conclusions because debug mode adds overhead.
Exercise
A widget uses only the available screen size but calls MediaQuery.of(context).size. What should you try first, and what should you verify afterward?
Show answer
Replace it with MediaQuery.sizeOf(context), then measure rebuilds and frame time to verify that unrelated MediaQuery changes no longer affect the widget and that the change improves the real screen.