mirror of
https://github.com/hanskokx/arcane_framework.git
synced 2026-05-14 10:29:06 +02:00
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:
+3
-40
@@ -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<ArcaneApp> createState() => _ArcaneAppState();
|
||||
}
|
||||
|
||||
class _ArcaneAppState extends State<ArcaneApp> 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,17 @@ class ArcaneThemeSwitcher extends StatefulWidget {
|
||||
State<ArcaneThemeSwitcher> createState() => _ArcaneThemeSwitcherState();
|
||||
}
|
||||
|
||||
class _ArcaneThemeSwitcherState extends State<ArcaneThemeSwitcher> {
|
||||
class _ArcaneThemeSwitcherState extends State<ArcaneThemeSwitcher>
|
||||
with WidgetsBindingObserver {
|
||||
late final StreamSubscription<ThemeMode> _themeModeSubscription;
|
||||
late final StreamSubscription<ThemeData> _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<ArcaneThemeSwitcher> {
|
||||
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<ArcaneThemeSwitcher> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user