2

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?

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35

1 Answers1

2

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"),
          ),
        ],
      ),
    );
  }
}
Cenk YAGMUR
  • 3,154
  • 2
  • 26
  • 42
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56