I have this example ChangeNotifier
:
class ExmapleNotifier extends ChangeNotifier {
bool isDark = false;
toggleTheme() {
isDark = !isDark;
notifyListeners();
}
}
when I try to use it with a ValueListenableBuilder
, it throws an error that it's not a ValueListenable
, is there any way to combine them both?
For this, I may go with ChangeNotifierProvider. You can also use AnimatedBuilder
widget.
class CHTest extends StatelessWidget {
const CHTest({super.key});
@override
Widget build(BuildContext context) {
final exampleNotifier = ExmapleNotifier();
return AnimatedBuilder(
animation: exampleNotifier,
builder: (context, child) => Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("is Dark ${exampleNotifier.isDark}"),
ElevatedButton(
onPressed: exampleNotifier.toggleTheme,
child: Text("toggle"),
),
],
),
);
}
}