Description (bug summary)
I was playing around with screen_brightness
to see if I can have real-time data on system brightness. My use case is to have app change to light/dark theme based on system's brightness (for instance, have dark mode activated automatically when system brightness dips below 0.30 — and activate light theme when the opposite is true).
It works, for so long as the app is not in the foreground/not focused — the moment the app window is focused, it doesn't update anymore (gets stuck with the last result) — put app in background, and now works fine again.
Tried both ScreenBrightness().current
and ScreenBrightness().system
to no avail.
The following is the code I played with:
FutureBuilder<double>(
future: ScreenBrightness().current,
//future: ScreenBrightness().system,
builder: (context, snapshot) {
double currentBrightness = 0;
if (snapshot.hasData) {
currentBrightness = snapshot.data!;
}
return StreamBuilder<double>(
stream: ScreenBrightness()
.onCurrentBrightnessChanged,
builder: (context, snapshot) {
double changedBrightness =
currentBrightness;
if (snapshot.hasData) {
changedBrightness = snapshot.data!;
}
return Text('Current brightness: $changedBrightness');
},
);
},
),
Also, with the following:
double _brightness = 0;
@override
void initState() {
super.initState();
_getBrightness();
_onCurrentBrightnessChanged();
}
Future<void> _getBrightness() async {
final brightness = await ScreenBrightness().system;
setState(() {
_brightness = brightness;
});
}
void _onCurrentBrightnessChanged() {
ScreenBrightness().onCurrentBrightnessChanged.listen((brightness) {
setState(() {
_brightness = brightness;
});
//print(_brightness);
});
}
but the result is the same.
Step to reproduce (how to reproduce the bug)
- Run example/lib/main.dart
- While the app is focused, change system brightness (doesn't work in my case)
- While the app is not focused, change system brightness (works great)
Platform (only tested on iOS and macOS)
The above steps apply to macOS (testing hardware: MBP 13' 2017, using macOS Ventura 13.0).
For iOS (testing hardware: iPhone X, using iOS 16.5), I was turning room lights on and off — and it was not updating, even though the screen's brightness was changing. Sidenote: While the app is running, if we open the control center and change system's brightness manually — it works, but it doesn't count because the app is losing focus this way. And the desired outcome is to follow system's brightness in real-time, while using the app.