-
Notifications
You must be signed in to change notification settings - Fork 104
/
change_notifier.dart
66 lines (59 loc) · 1.76 KB
/
change_notifier.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import 'package:flutter/material.dart';
import 'controller.dart';
class MyChangeNotifier extends StatefulWidget {
const MyChangeNotifier({Key? key}) : super(key: key);
@override
State<MyChangeNotifier> createState() => _MyChangeNotifierState();
}
class _MyChangeNotifierState extends State<MyChangeNotifier> {
final controller = CNController();
int get _counter => controller.counter;
@override
void initState() {
debugPrint('initState executed once');
super.initState();
controller.addListener(() => setState(() {}));
}
@override
Widget build(BuildContext context) {
debugPrint('build executed once');
return Scaffold(
appBar: AppBar(title: const Text('Change Notifier')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Incrementing & Decrementing\nusing\nChange Notifier',
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
const SizedBox(height: 20),
Text(
'$_counter',
style: const TextStyle(
color: Colors.grey,
fontSize: 60,
fontWeight: FontWeight.bold,
),
),
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
onPressed: () => controller.decrement(),
child: const Icon(Icons.remove),
),
const SizedBox(width: 20),
FloatingActionButton(
onPressed: () => controller.increment(),
child: const Icon(Icons.add),
),
],
),
);
}
}