Status bar icons that read the room
You might've used Flutter's AnnotatedRegion to change the navigation bar color or the status bar icon color for a whole screen or the whole app. Wrap the widget, pass a
SystemUiOverlayStyle, done.
What's less known is that it doesn't have to be the whole screen. You can annotate specific widgets, and Flutter will pick the style of whatever is currently under the status bar (and the navigation bar on Android). Scroll a dark widget under it and the icons go light, scroll a light one and they go dark. No listeners, no scroll offsets:
Here's a list of black and white items, each annotated with its own style:
const _onDark = SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.light, // Android
statusBarBrightness: Brightness.dark, // iOS
systemNavigationBarColor: Colors.transparent,
systemNavigationBarIconBrightness: Brightness.light,
);
const _onLight = SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.dark,
statusBarBrightness: Brightness.light,
systemNavigationBarColor: Colors.transparent,
systemNavigationBarIconBrightness: Brightness.dark,
);
ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
final isDark = index.isEven;
return AnnotatedRegion<SystemUiOverlayStyle>(
value: isDark ? _onDark : _onLight,
child: Container(
height: 300,
color: isDark ? Colors.black : Colors.white,
),
);
},
);
That's it. On every frame Flutter looks up the annotated region at the top of the screen and at the bottom, and applies the style it finds there.
Neat!
Notes
-
An
AppBaradds its ownAnnotatedRegion, so if it sits under the status bar it wins. -
statusBarBrightness(iOS) means the brightness of the background, not the icons, so it's the opposite ofstatusBarIconBrightness. - Transparent bars only make sense when the app draws edge-to-edge, otherwise there's nothing under them to react to.