Conversation
of the app's theme when the platform changes it's brightness.
|
How does the app get notified when the system brightness changes? And how is the |
|
@Norbert515 , There was an API to be notified via callback, however, it required platform integrations and it only worked on Android Q. This was a complex solution, so I found out that there's a way to check for brightness. It's a simple builder that always rebuilds with the current platform Brightness setting. There's no notification, it just stays current anytime the widget tree rebuilds. I couldn't find anything to listen to unless I created my own stream, but it would only be updated during a build. I tested it by putting a breakpoint in the builder of the |
What do you mean by that? I didn't make it a part of the existing widget because of separation of concerns. |
|
Sounds good! But should the |
|
That should be up to the user. I believe it's only job should be to provide the current value, like orientation builders |
|
What about didChangePlatformBrightness and onPlatformBrightnessChanged? |
|
Oh, Nevermind. These are all methods. We're trying to provide a widget with the most recent value |
|
Sure, you can use |
|
I don't see how it's better than my suggestion solution. When the platformBrightness changes, MediaQuery rebuilds its descendants with that, which my widget depends on, so it works. No callbacks needed |
|
Okay so if I understand this correctly, the |
That's what the original |
|
I forked the repo and added your changes to it. I also added to the example. I think I'm doing it wrong but your builder always returned |
|
What are you doing to view the changes? You have to go into system settings on the phone and change the theme to dark mode . This is an android pie or q thing |
|
I'm running an emulator w/ Android 10 and changing the theme to dark mode. |
OK, let me retest |
|
Can I see your code? |
|
Sure, I just pushed another commit with the example added |
|
Alright, I see. Only issue I see is that it doesn't work above MaterialApp so users are going to have to wrap all of their routes with a consumer or the |
|
OK, I was expecting to see an error. It does show Light for me as well. One sec |
|
I don't know how to resolve this cause it requires a WidgetsApp class above it. |
|
How does Flutter do it with |
|
Because they access the variable you set there or default it to ThemeMode.system. Inside the widget, it uses MediaQuery.platformBrightnessOf(context); which works because this is separated by a builder and the MediaQuery is already in the widget tree by that time. builder: (BuildContext context, Widget child) {
// Use a light theme, dark theme, or fallback theme.
final ThemeMode mode = widget.themeMode ?? ThemeMode.system;
ThemeData theme;
if (widget.darkTheme != null) {
final ui.Brightness platformBrightness = MediaQuery.platformBrightnessOf(context);
if (mode == ThemeMode.dark ||
(mode == ThemeMode.system && platformBrightness == ui.Brightness.dark)) {
theme = widget.darkTheme;
}
} |
|
@Reprevise best thing I can do is make a warning or throw an error if it's put above the MaterialApp, mentioning that there's no ancestor found, since MediaQuery by default returns Brightness.light if it can't find the value |
|
Alright, sounds like the best option |
|
@ThinkDigitalSoftware Conflicting files |



Add PlatformBrightness widget to allow automatic updating of the
app's theme when the platform changes it's brightness.