From bfa5c646d7ef8b9d96f0f798c6558ba2a2ca0b32 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Mon, 28 Apr 2025 14:31:52 +0200 Subject: [PATCH] Fixes bug with following/unfollowing system theme Signed-off-by: Hans Kokx --- lib/arcane_framework.dart | 2 +- .../services/reactive_theme/arcane_theme.dart | 23 ++++++++++ .../reactive_theme_switcher.dart | 16 +++---- .../reactive_theme_wrapper.dart | 42 ------------------- 4 files changed, 30 insertions(+), 53 deletions(-) create mode 100644 lib/src/services/reactive_theme/arcane_theme.dart delete mode 100644 lib/src/services/reactive_theme/reactive_theme_wrapper.dart diff --git a/lib/arcane_framework.dart b/lib/arcane_framework.dart index a8f02b9..308b285 100644 --- a/lib/arcane_framework.dart +++ b/lib/arcane_framework.dart @@ -44,6 +44,6 @@ export "package:arcane_framework/src/providers/service_provider.dart"; export "package:arcane_framework/src/services/authentication/authentication_service.dart"; export "package:arcane_framework/src/services/feature_flags/feature_flags_service.dart"; export "package:arcane_framework/src/services/logging/logging_service.dart"; +export "package:arcane_framework/src/services/reactive_theme/arcane_theme.dart"; export "package:arcane_framework/src/services/reactive_theme/reactive_theme_service.dart"; -export "package:arcane_framework/src/services/reactive_theme/reactive_theme_wrapper.dart"; export "package:result_monad/result_monad.dart"; diff --git a/lib/src/services/reactive_theme/arcane_theme.dart b/lib/src/services/reactive_theme/arcane_theme.dart new file mode 100644 index 0000000..f3a5bd4 --- /dev/null +++ b/lib/src/services/reactive_theme/arcane_theme.dart @@ -0,0 +1,23 @@ +import "package:flutter/material.dart"; + +class ArcaneTheme extends InheritedWidget { + final ThemeMode themeMode; + final bool followSystem; + + const ArcaneTheme({ + required this.themeMode, + required this.followSystem, + required super.child, + super.key, + }); + + static ArcaneTheme? of(BuildContext context) { + return context.dependOnInheritedWidgetOfExactType(); + } + + @override + bool updateShouldNotify(ArcaneTheme oldWidget) { + return themeMode != oldWidget.themeMode || + followSystem != oldWidget.followSystem; + } +} diff --git a/lib/src/services/reactive_theme/reactive_theme_switcher.dart b/lib/src/services/reactive_theme/reactive_theme_switcher.dart index bd50370..94d053a 100644 --- a/lib/src/services/reactive_theme/reactive_theme_switcher.dart +++ b/lib/src/services/reactive_theme/reactive_theme_switcher.dart @@ -16,31 +16,27 @@ class ArcaneThemeSwitcher extends StatefulWidget { } class _ArcaneThemeSwitcherState extends State { - late final StreamSubscription _themeModeSubscription; - ThemeMode _currentThemeMode = ArcaneReactiveTheme.I.currentTheme; + late final StreamSubscription _subscription; @override void initState() { super.initState(); - _themeModeSubscription = - ArcaneReactiveTheme.I.themeChanges.listen((ThemeMode newMode) { - if (mounted) - setState(() { - _currentThemeMode = newMode; - }); + _subscription = ArcaneReactiveTheme.I.themeChanges.listen((_) { + setState(() {}); }); } @override void dispose() { - _themeModeSubscription.cancel(); + _subscription.cancel(); super.dispose(); } @override Widget build(BuildContext context) { return ArcaneTheme( - themeMode: _currentThemeMode, + themeMode: ArcaneReactiveTheme.I.currentTheme, + followSystem: ArcaneReactiveTheme.I.isFollowingSystemTheme, child: widget.child, ); } diff --git a/lib/src/services/reactive_theme/reactive_theme_wrapper.dart b/lib/src/services/reactive_theme/reactive_theme_wrapper.dart deleted file mode 100644 index f51c370..0000000 --- a/lib/src/services/reactive_theme/reactive_theme_wrapper.dart +++ /dev/null @@ -1,42 +0,0 @@ -import "dart:async"; - -import "package:arcane_framework/src/services/reactive_theme/reactive_theme_service.dart"; -import "package:flutter/material.dart"; - -class ArcaneTheme extends InheritedWidget { - final ThemeMode themeMode; - - const ArcaneTheme({ - required this.themeMode, - required super.child, - super.key, - }); - - static ArcaneTheme? of(BuildContext context) { - return context.dependOnInheritedWidgetOfExactType(); - } - - @override - bool updateShouldNotify(ArcaneTheme oldWidget) { - return themeMode != oldWidget.themeMode; - } - - static ArcaneReactiveTheme get service => ArcaneReactiveTheme.I; - static bool get isFollowingSystemTheme => service.isFollowingSystemTheme; - static Stream get themeChanges => service.themeChanges; - static ThemeMode get currentTheme => service.currentTheme; - static ThemeMode get systemTheme => service.systemTheme; - static ThemeData get dark => service.dark; - static ValueNotifier get darkTheme => service.darkTheme; - static ThemeData get light => service.light; - static ValueNotifier get lightTheme => service.lightTheme; - - static ArcaneReactiveTheme Function({ThemeMode? themeMode}) get switchTheme => - service.switchTheme; - static ArcaneReactiveTheme Function(BuildContext context) - get followSystemTheme => service.followSystemTheme; - static ArcaneReactiveTheme Function(ThemeData theme) get setDarkTheme => - service.setDarkTheme; - static ArcaneReactiveTheme Function(ThemeData theme) get setLightTheme => - service.setLightTheme; -}