forked from md-siam/widget_of_the_day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstateful_widget.dart
96 lines (87 loc) · 2.34 KB
/
stateful_widget.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import 'package:flutter/material.dart';
import 'first_page.dart';
class MyStatufulWidget extends StatefulWidget {
final int number;
const MyStatufulWidget({Key? key, required this.number}) : super(key: key);
@override
_MyStatufulWidgetState createState() {
print('Number: $number CreateState');
return _MyStatufulWidgetState();
}
}
class _MyStatufulWidgetState extends State<MyStatufulWidget> {
int number = 0;
// first lifecycle
@override
void initState() {
super.initState();
number = widget.number;
print('Number: ${widget.number} InitState');
}
// second lifecycle
@override
void didChangeDependencies() {
super.didChangeDependencies();
print('Number: ${widget.number} DidChangeDependencies');
}
// check update done in previous page
@override
void didUpdateWidget(MyStatufulWidget oldWidget) {
super.didUpdateWidget(oldWidget);
print('Number: ${widget.number} DidUpdateWidget');
if (oldWidget.number != widget.number) {
print('Number has changed');
}
}
// rarely used
@override
void deactivate() {
print('Number: ${widget.number} Deactivate');
super.deactivate();
}
// dispose method
@override
void dispose() {
print('Number: ${widget.number} Dispose');
super.dispose();
}
@override
Widget build(BuildContext context) {
print('Number: $number Build');
return Scaffold(
appBar: AppBar(title: const Text('Stateful Widget')),
body: Center(
child: Column(
children: [
SizedBox(
height: 500,
width: 350,
child: TextButton(
child: Text(
number.toString(),
style: const TextStyle(fontSize: 80),
),
onPressed: () {
setState(() {
print('Number: ${widget.number} SetState');
number++;
});
},
),
),
ElevatedButton(
child: const Text('First Page'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FirstPage(numberFirst: number)),
);
},
),
],
),
),
);
}
}