mirror of
https://github.com/hanskokx/arcane_framework.git
synced 2026-05-14 10:29:06 +02:00
Refactor theme management to use ListenableBuilder for dynamic theme updates
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
+14
-12
@@ -269,9 +269,14 @@ class ArcaneThemeExample extends StatelessWidget {
|
|||||||
const ArcaneThemeExample({
|
const ArcaneThemeExample({
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
return ListenableBuilder(
|
||||||
|
listenable: Arcane.theme.themeChanges,
|
||||||
|
builder: (context, _) {
|
||||||
|
final ThemeData effectiveTheme = Arcane.theme.currentTheme;
|
||||||
|
final bool isFollowingSystem = Arcane.theme.isFollowingSystemTheme;
|
||||||
|
|
||||||
return Card(
|
return Card(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
@@ -294,7 +299,8 @@ class ArcaneThemeExample extends StatelessWidget {
|
|||||||
return const Icon(Icons.light_mode);
|
return const Icon(Icons.light_mode);
|
||||||
}),
|
}),
|
||||||
onChanged: (_) {
|
onChanged: (_) {
|
||||||
final ThemeMode oldTheme = Arcane.theme.currentThemeMode;
|
final ThemeMode oldTheme =
|
||||||
|
Arcane.theme.currentThemeMode;
|
||||||
Arcane.theme.switchTheme();
|
Arcane.theme.switchTheme();
|
||||||
Arcane.log(
|
Arcane.log(
|
||||||
"Switching theme",
|
"Switching theme",
|
||||||
@@ -310,9 +316,7 @@ class ArcaneThemeExample extends StatelessWidget {
|
|||||||
Row(
|
Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
ValueListenableBuilder<bool>(
|
Checkbox(
|
||||||
valueListenable: Arcane.theme.followingSystemThemeChanges,
|
|
||||||
builder: (context, isFollowingSystem, _) => Checkbox(
|
|
||||||
value: isFollowingSystem,
|
value: isFollowingSystem,
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
final ThemeMode oldTheme =
|
final ThemeMode oldTheme =
|
||||||
@@ -344,7 +348,6 @@ class ArcaneThemeExample extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
|
||||||
const Text("Follow system"),
|
const Text("Follow system"),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -358,10 +361,7 @@ class ArcaneThemeExample extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
const Text("Color"),
|
const Text("Color"),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: ValueListenableBuilder<ThemeData>(
|
child: ListView.separated(
|
||||||
valueListenable: Arcane.theme.effectiveThemeChanges,
|
|
||||||
builder: (context, effectiveTheme, _) =>
|
|
||||||
ListView.separated(
|
|
||||||
itemCount: colors.length,
|
itemCount: colors.length,
|
||||||
scrollDirection: Axis.horizontal,
|
scrollDirection: Axis.horizontal,
|
||||||
separatorBuilder: (_, __) => const SizedBox(width: 4),
|
separatorBuilder: (_, __) => const SizedBox(width: 4),
|
||||||
@@ -375,7 +375,8 @@ class ArcaneThemeExample extends StatelessWidget {
|
|||||||
colorSchemeSeed: colors[index],
|
colorSchemeSeed: colors[index],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else if (context.themeMode == ThemeMode.light) {
|
} else if (context.themeMode ==
|
||||||
|
ThemeMode.light) {
|
||||||
Arcane.theme.setLightTheme(
|
Arcane.theme.setLightTheme(
|
||||||
ThemeData(
|
ThemeData(
|
||||||
brightness: Brightness.light,
|
brightness: Brightness.light,
|
||||||
@@ -421,7 +422,6 @@ class ArcaneThemeExample extends StatelessWidget {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -434,6 +434,8 @@ class ArcaneThemeExample extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,6 +82,16 @@ class ArcaneReactiveTheme extends ArcaneService {
|
|||||||
final ValueNotifier<bool> _followingSystemThemeNotifier =
|
final ValueNotifier<bool> _followingSystemThemeNotifier =
|
||||||
ValueNotifier<bool>(false);
|
ValueNotifier<bool>(false);
|
||||||
|
|
||||||
|
/// Combined Listenable that merges all theme-related changes.
|
||||||
|
/// Use this for widgets that need to rebuild on any theme change.
|
||||||
|
Listenable get themeChanges => I._combinedThemeListenable;
|
||||||
|
|
||||||
|
late final Listenable _combinedThemeListenable = Listenable.merge([
|
||||||
|
_effectiveThemeNotifier,
|
||||||
|
_followingSystemThemeNotifier,
|
||||||
|
_themeModeNotifier,
|
||||||
|
]);
|
||||||
|
|
||||||
// ************************************************************************ //
|
// ************************************************************************ //
|
||||||
// * MARK: Light/Dark theme
|
// * MARK: Light/Dark theme
|
||||||
// ************************************************************************ //
|
// ************************************************************************ //
|
||||||
|
|||||||
Reference in New Issue
Block a user