Moved platform brightness checking from ArcaneApp to ArcaneThemeSwitcher, where it is more appropriate

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2025-04-29 13:25:37 +02:00
parent 599ade3981
commit c5c0009cb4
2 changed files with 25 additions and 41 deletions
+3 -40
View File
@@ -19,7 +19,7 @@ import "package:flutter/material.dart";
/// child: MyApp(), /// child: MyApp(),
/// ); /// );
/// ``` /// ```
class ArcaneApp extends StatefulWidget { class ArcaneApp extends StatelessWidget {
/// A list of Arcane services that will be made available to the application. /// A list of Arcane services that will be made available to the application.
/// ///
/// These services will be provided to the widget tree using /// These services will be provided to the widget tree using
@@ -43,52 +43,15 @@ class ArcaneApp extends StatefulWidget {
super.key, super.key,
}); });
@override
State<ArcaneApp> createState() => _ArcaneAppState();
}
class _ArcaneAppState extends State<ArcaneApp> with WidgetsBindingObserver {
final GlobalKey _appKey = GlobalKey();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ArcaneEnvironmentProvider( return ArcaneEnvironmentProvider(
child: ArcaneServiceProvider( child: ArcaneServiceProvider(
serviceInstances: widget.services, serviceInstances: services,
child: ArcaneThemeSwitcher( child: ArcaneThemeSwitcher(
key: _appKey, child: child,
child: widget.child,
), ),
), ),
); );
} }
@override
void initState() {
super.initState();
// Register as an observer to detect system theme changes
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
// Clean up the observer when the widget is disposed
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangePlatformBrightness() {
// When system brightness changes, find the current builder context
// and use it to check the system theme
if (mounted && _appKey.currentContext != null) {
// Use the current context from the key to check system theme
if (ArcaneReactiveTheme.I.isFollowingSystemTheme) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ArcaneReactiveTheme.I.followSystemTheme(context);
});
}
}
super.didChangePlatformBrightness();
}
} }
@@ -15,13 +15,17 @@ class ArcaneThemeSwitcher extends StatefulWidget {
State<ArcaneThemeSwitcher> createState() => _ArcaneThemeSwitcherState(); State<ArcaneThemeSwitcher> createState() => _ArcaneThemeSwitcherState();
} }
class _ArcaneThemeSwitcherState extends State<ArcaneThemeSwitcher> { class _ArcaneThemeSwitcherState extends State<ArcaneThemeSwitcher>
with WidgetsBindingObserver {
late final StreamSubscription<ThemeMode> _themeModeSubscription; late final StreamSubscription<ThemeMode> _themeModeSubscription;
late final StreamSubscription<ThemeData> _themeSubscription; late final StreamSubscription<ThemeData> _themeSubscription;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
// Register as an observer to detect system theme changes
WidgetsBinding.instance.addObserver(this);
_themeModeSubscription = ArcaneReactiveTheme.I.themeModeChanges.listen((_) { _themeModeSubscription = ArcaneReactiveTheme.I.themeModeChanges.listen((_) {
setState(() {}); setState(() {});
}); });
@@ -34,6 +38,8 @@ class _ArcaneThemeSwitcherState extends State<ArcaneThemeSwitcher> {
void dispose() { void dispose() {
_themeModeSubscription.cancel(); _themeModeSubscription.cancel();
_themeSubscription.cancel(); _themeSubscription.cancel();
// Clean up the observer when the widget is disposed
WidgetsBinding.instance.removeObserver(this);
super.dispose(); super.dispose();
} }
@@ -46,4 +52,19 @@ class _ArcaneThemeSwitcherState extends State<ArcaneThemeSwitcher> {
child: widget.child, child: widget.child,
); );
} }
@override
void didChangePlatformBrightness() {
// When system brightness changes, find the current builder context
// and use it to check the system theme
if (mounted) {
// Use the current context from the key to check system theme
if (ArcaneReactiveTheme.I.isFollowingSystemTheme) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ArcaneReactiveTheme.I.followSystemTheme(context);
});
}
}
super.didChangePlatformBrightness();
}
} }