From c5c0009cb44201697ef0139bdf757a814811658c Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Tue, 29 Apr 2025 13:25:37 +0200 Subject: [PATCH] Moved platform brightness checking from ArcaneApp to ArcaneThemeSwitcher, where it is more appropriate Signed-off-by: Hans Kokx --- lib/src/arcane_app.dart | 43 ++----------------- .../reactive_theme_switcher.dart | 23 +++++++++- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/lib/src/arcane_app.dart b/lib/src/arcane_app.dart index 9371d2e..e263815 100644 --- a/lib/src/arcane_app.dart +++ b/lib/src/arcane_app.dart @@ -19,7 +19,7 @@ import "package:flutter/material.dart"; /// child: MyApp(), /// ); /// ``` -class ArcaneApp extends StatefulWidget { +class ArcaneApp extends StatelessWidget { /// A list of Arcane services that will be made available to the application. /// /// These services will be provided to the widget tree using @@ -43,52 +43,15 @@ class ArcaneApp extends StatefulWidget { super.key, }); - @override - State createState() => _ArcaneAppState(); -} - -class _ArcaneAppState extends State with WidgetsBindingObserver { - final GlobalKey _appKey = GlobalKey(); - @override Widget build(BuildContext context) { return ArcaneEnvironmentProvider( child: ArcaneServiceProvider( - serviceInstances: widget.services, + serviceInstances: services, child: ArcaneThemeSwitcher( - key: _appKey, - child: widget.child, + child: 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(); - } } diff --git a/lib/src/services/reactive_theme/reactive_theme_switcher.dart b/lib/src/services/reactive_theme/reactive_theme_switcher.dart index 4913e26..99689f4 100644 --- a/lib/src/services/reactive_theme/reactive_theme_switcher.dart +++ b/lib/src/services/reactive_theme/reactive_theme_switcher.dart @@ -15,13 +15,17 @@ class ArcaneThemeSwitcher extends StatefulWidget { State createState() => _ArcaneThemeSwitcherState(); } -class _ArcaneThemeSwitcherState extends State { +class _ArcaneThemeSwitcherState extends State + with WidgetsBindingObserver { late final StreamSubscription _themeModeSubscription; late final StreamSubscription _themeSubscription; @override void initState() { super.initState(); + // Register as an observer to detect system theme changes + WidgetsBinding.instance.addObserver(this); + _themeModeSubscription = ArcaneReactiveTheme.I.themeModeChanges.listen((_) { setState(() {}); }); @@ -34,6 +38,8 @@ class _ArcaneThemeSwitcherState extends State { void dispose() { _themeModeSubscription.cancel(); _themeSubscription.cancel(); + // Clean up the observer when the widget is disposed + WidgetsBinding.instance.removeObserver(this); super.dispose(); } @@ -46,4 +52,19 @@ class _ArcaneThemeSwitcherState extends State { 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(); + } }