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:
2025-07-01 17:05:05 +02:00
parent b0c9fe7b98
commit 707a4c7956
2 changed files with 166 additions and 154 deletions
+156 -154
View File
@@ -269,170 +269,172 @@ class ArcaneThemeExample extends StatelessWidget {
const ArcaneThemeExample({ const ArcaneThemeExample({
super.key, super.key,
}); });
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Card( return ListenableBuilder(
child: Padding( listenable: Arcane.theme.themeChanges,
padding: const EdgeInsets.all(8.0), builder: (context, _) {
child: Column( final ThemeData effectiveTheme = Arcane.theme.currentTheme;
mainAxisAlignment: MainAxisAlignment.spaceBetween, final bool isFollowingSystem = Arcane.theme.isFollowingSystemTheme;
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [ return Card(
Text( child: Padding(
"Theme", padding: const EdgeInsets.all(8.0),
style: Theme.of(context).textTheme.headlineSmall, child: Column(
), mainAxisAlignment: MainAxisAlignment.spaceBetween,
Column( crossAxisAlignment: CrossAxisAlignment.stretch,
children: [ children: [
Switch( Text(
value: Arcane.theme.currentThemeMode == ThemeMode.dark, "Theme",
thumbIcon: WidgetStateProperty.resolveWith((states) { style: Theme.of(context).textTheme.headlineSmall,
if (states.contains(WidgetState.selected)) {
return const Icon(Icons.dark_mode);
}
return const Icon(Icons.light_mode);
}),
onChanged: (_) {
final ThemeMode oldTheme = Arcane.theme.currentThemeMode;
Arcane.theme.switchTheme();
Arcane.log(
"Switching theme",
metadata: {
"followingSystemTheme":
"${Arcane.theme.isFollowingSystemTheme}",
"newMode": Arcane.theme.currentThemeMode.name,
"oldMode": oldTheme.name,
},
);
},
), ),
Row( Column(
mainAxisSize: MainAxisSize.min,
children: [ children: [
ValueListenableBuilder<bool>( Switch(
valueListenable: Arcane.theme.followingSystemThemeChanges, value: Arcane.theme.currentThemeMode == ThemeMode.dark,
builder: (context, isFollowingSystem, _) => Checkbox( thumbIcon: WidgetStateProperty.resolveWith((states) {
value: isFollowingSystem, if (states.contains(WidgetState.selected)) {
onChanged: (value) { return const Icon(Icons.dark_mode);
final ThemeMode oldTheme = }
Arcane.theme.currentThemeMode; return const Icon(Icons.light_mode);
if (value == true) { }),
Arcane.theme.followSystemTheme(context); onChanged: (_) {
Arcane.log( final ThemeMode oldTheme =
"Switching theme", Arcane.theme.currentThemeMode;
metadata: { Arcane.theme.switchTheme();
"followingSystemTheme": Arcane.log(
"${Arcane.theme.isFollowingSystemTheme}", "Switching theme",
"newMode": Arcane.theme.currentThemeMode.name, metadata: {
"oldMode": oldTheme.name, "followingSystemTheme":
}, "${Arcane.theme.isFollowingSystemTheme}",
); "newMode": Arcane.theme.currentThemeMode.name,
} else { "oldMode": oldTheme.name,
Arcane.theme.switchTheme( },
themeMode: Arcane.theme.systemThemeMode, );
); },
Arcane.log( ),
"Switching theme", Row(
metadata: { mainAxisSize: MainAxisSize.min,
"followingSystemTheme": children: [
"${Arcane.theme.isFollowingSystemTheme}", Checkbox(
"newMode": Arcane.theme.currentThemeMode.name, value: isFollowingSystem,
"oldMode": oldTheme.name, onChanged: (value) {
}, final ThemeMode oldTheme =
); Arcane.theme.currentThemeMode;
} if (value == true) {
}, Arcane.theme.followSystemTheme(context);
), Arcane.log(
"Switching theme",
metadata: {
"followingSystemTheme":
"${Arcane.theme.isFollowingSystemTheme}",
"newMode": Arcane.theme.currentThemeMode.name,
"oldMode": oldTheme.name,
},
);
} else {
Arcane.theme.switchTheme(
themeMode: Arcane.theme.systemThemeMode,
);
Arcane.log(
"Switching theme",
metadata: {
"followingSystemTheme":
"${Arcane.theme.isFollowingSystemTheme}",
"newMode": Arcane.theme.currentThemeMode.name,
"oldMode": oldTheme.name,
},
);
}
},
),
const Text("Follow system"),
],
), ),
const Text("Follow system"),
], ],
), ),
SizedBox(
height: 20,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
spacing: 8,
children: [
const Text("Color"),
Expanded(
child: ListView.separated(
itemCount: colors.length,
scrollDirection: Axis.horizontal,
separatorBuilder: (_, __) => const SizedBox(width: 4),
itemBuilder: (context, index) {
return InkWell(
onTap: () {
if (context.themeMode == ThemeMode.dark) {
Arcane.theme.setDarkTheme(
ThemeData(
brightness: Brightness.dark,
colorSchemeSeed: colors[index],
),
);
} else if (context.themeMode ==
ThemeMode.light) {
Arcane.theme.setLightTheme(
ThemeData(
brightness: Brightness.light,
colorSchemeSeed: colors[index],
),
);
}
Arcane.log(
"Setting ${Arcane.theme.currentThemeMode.name} theme color to ${colors[index].name}",
);
},
child: Container(
key: Key(
"${colors[index]}-${Arcane.theme.currentThemeMode}"),
decoration: BoxDecoration(
color: colors[index],
border:
effectiveTheme.colorScheme.primary.name ==
colors[index].name
? Border.all(
width: 2,
color: Colors.white,
)
: null,
boxShadow:
effectiveTheme.colorScheme.primary.name ==
colors[index].name
? [
const BoxShadow(
color: Colors.black,
spreadRadius: 1,
blurRadius: 1,
offset: Offset(0, 0),
),
]
: null,
),
width: 20,
height: 20,
),
);
},
),
),
],
),
),
Text(
"The current theme mode is ${Arcane.theme.currentModeOf(context).name} and "
"is ${Arcane.theme.isFollowingSystemTheme ? "" : "not "}"
"following the system theme.",
),
], ],
), ),
SizedBox( ),
height: 20, );
child: Row( },
mainAxisAlignment: MainAxisAlignment.spaceBetween,
spacing: 8,
children: [
const Text("Color"),
Expanded(
child: ValueListenableBuilder<ThemeData>(
valueListenable: Arcane.theme.effectiveThemeChanges,
builder: (context, effectiveTheme, _) =>
ListView.separated(
itemCount: colors.length,
scrollDirection: Axis.horizontal,
separatorBuilder: (_, __) => const SizedBox(width: 4),
itemBuilder: (context, index) {
return InkWell(
onTap: () {
if (context.themeMode == ThemeMode.dark) {
Arcane.theme.setDarkTheme(
ThemeData(
brightness: Brightness.dark,
colorSchemeSeed: colors[index],
),
);
} else if (context.themeMode == ThemeMode.light) {
Arcane.theme.setLightTheme(
ThemeData(
brightness: Brightness.light,
colorSchemeSeed: colors[index],
),
);
}
Arcane.log(
"Setting ${Arcane.theme.currentThemeMode.name} theme color to ${colors[index].name}",
);
},
child: Container(
key: Key(
"${colors[index]}-${Arcane.theme.currentThemeMode}"),
decoration: BoxDecoration(
color: colors[index],
border:
effectiveTheme.colorScheme.primary.name ==
colors[index].name
? Border.all(
width: 2,
color: Colors.white,
)
: null,
boxShadow:
effectiveTheme.colorScheme.primary.name ==
colors[index].name
? [
const BoxShadow(
color: Colors.black,
spreadRadius: 1,
blurRadius: 1,
offset: Offset(0, 0),
),
]
: null,
),
width: 20,
height: 20,
),
);
},
),
),
),
],
),
),
Text(
"The current theme mode is ${Arcane.theme.currentModeOf(context).name} and "
"is ${Arcane.theme.isFollowingSystemTheme ? "" : "not "}"
"following the system theme.",
),
],
),
),
); );
} }
} }
@@ -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
// ************************************************************************ // // ************************************************************************ //