In my case, a simple app should automatically use the theme (light or dark) of the system to style the user interface. By default, this should work when using ThemeMode.system
(see flutter documentation). But it didn’t.
The themes have been defined as follows:
Dart
return MaterialApp(
themeMode: ThemeMode.system,
theme: ThemeData( ... ),
darkTheme: ThemeData( ...),
...
);
In addition, the WidgetsBindingObserver
callback didChangePlatformBrightness()
was never called. It was defined as follows:
Dart
class MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver
{
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangePlatformBrightness() {
print(WidgetsBinding.instance.window.platformBrightness);
// > should print Brightness.light / Brightness.dark when you switch
super.didChangePlatformBrightness();
}
}
After hours and days of searching, it turned out, that the following definition was set in info.plist
of iOS:
XML
<key>UIUserInterfaceStyle</key>
<string>Light</string>
Removing this line solved the issue. This setting sets the apps theme to Light, which results in a constant value even if the user changed the brightness to dark. Without this line, UIUserInterfaceStyle
depends on the global setting.
Leave a Reply