ChangeNotifier:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class Counter extends ChangeNotifier {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
void decrement() {
_count--;
notifyListeners();
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => Counter(),
child: MaterialApp(
home: CounterScreen(),
),
);
}
}
class CounterScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counter = Provider.of
(context);
return Scaffold(
appBar: AppBar(title: Text('ChangeNotifier Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Count: ${counter.count}', style: TextStyle(fontSize: 24)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.remove),
onPressed: counter.decrement,
),
IconButton(
icon: Icon(Icons.add),
onPressed: counter.increment,
),
],
),
],
),
),
);
}
}
ValueNotifier:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: CounterScreen(),
);
}
}
class CounterScreen extends StatelessWidget {
final ValueNotifier
_counter = ValueNotifier (0); @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('ValueNotifier Example')),
body: Center(
child: ValueListenableBuilder
( valueListenable: _counter,
builder: (context, count, _) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Count: $count', style: TextStyle(fontSize: 24)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.remove),
onPressed: () => _counter.value--,
),
IconButton(
icon: Icon(Icons.add),
onPressed: () => _counter.value++,
),
],
),
],
);
},
),
),
);
}
}
StatefulWidget:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: CounterScreen(),
);
}
}
class CounterScreen extends StatefulWidget {
@override
_CounterScreenState createState() => _CounterScreenState();
}
class _CounterScreenState extends State
{ int _count = 0;
void _increment() {
setState(() {
_count++;
});
}
void _decrement() {
setState(() {
_count--;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('StatefulWidget Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Count: $_count', style: TextStyle(fontSize: 24)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.remove),
onPressed: _decrement,
),
IconButton(
icon: Icon(Icons.add),
onPressed: _increment,
),
],
),
],
),
),
);
}
}
When to Use ChangeNotifier vs. ValueNotifier:
Additional Considerations:
In essence:
Choose the approach that best suits your state management needs based on complexity, control, and desired notification behavior.
2024-09-12 03:13:34
Has anyone ever tested REAL performance difference between using Provider/ChangeNotifer with 1 values, vs ValueNotifier on a single value? I am curious, and a beginner so it's not obvious to be how to test such a thing.
I may try to create one