@@ -13,6 +13,15 @@ class MyApp extends StatelessWidget {
13
13
return MaterialApp (
14
14
title: 'Flutter Demo' ,
15
15
theme: ThemeData (
16
+ // This is the theme of your application.
17
+ //
18
+ // Try running your application with "flutter run". You'll see the
19
+ // application has a blue toolbar. Then, without quitting the app, try
20
+ // changing the primarySwatch below to Colors.green and then invoke
21
+ // "hot reload" (press "r" in the console where you ran "flutter run",
22
+ // or simply save your changes to "hot reload" in a Flutter IDE).
23
+ // Notice that the counter didn't reset back to zero; the application
24
+ // is not restarted.
16
25
primarySwatch: Colors .blue,
17
26
),
18
27
home: const MyHomePage (title: 'Flutter Demo Home Page' ),
@@ -23,13 +32,24 @@ class MyApp extends StatelessWidget {
23
32
class MyHomePage extends StatefulWidget {
24
33
const MyHomePage ({Key ? key, required this .title}) : super (key: key);
25
34
35
+ // This widget is the home page of your application. It is stateful, meaning
36
+ // that it has a State object (defined below) that contains fields that affect
37
+ // how it looks.
38
+
39
+ // This class is the configuration for the state. It holds the values (in this
40
+ // case the title) provided by the parent (in this case the App widget) and
41
+ // used by the build method of the State. Fields in a Widget subclass are
42
+ // always marked "final".
43
+
26
44
final String title;
27
45
28
46
@override
29
47
State <MyHomePage > createState () => _MyHomePageState ();
30
48
}
31
49
32
50
class _MyHomePageState extends State <MyHomePage > {
51
+ int _counter = 0 ;
52
+
33
53
String get _buildName {
34
54
return const String .fromEnvironment ('FLUTTER_BUILD_NAME' );
35
55
}
@@ -38,16 +58,58 @@ class _MyHomePageState extends State<MyHomePage> {
38
58
return const String .fromEnvironment ('FLUTTER_BUILD_NUMBER' );
39
59
}
40
60
61
+ void _incrementCounter () {
62
+ setState (() {
63
+ // This call to setState tells the Flutter framework that something has
64
+ // changed in this State, which causes it to rerun the build method below
65
+ // so that the display can reflect the updated values. If we changed
66
+ // _counter without calling setState(), then the build method would not be
67
+ // called again, and so nothing would appear to happen.
68
+ _counter++ ;
69
+ });
70
+ }
71
+
41
72
@override
42
73
Widget build (BuildContext context) {
74
+ // This method is rerun every time setState is called, for instance as done
75
+ // by the _incrementCounter method above.
76
+ //
77
+ // The Flutter framework has been optimized to make rerunning build methods
78
+ // fast, so that you can just rebuild anything that needs updating rather
79
+ // than having to individually change instances of widgets.
43
80
return Scaffold (
44
81
appBar: AppBar (
82
+ // Here we take the value from the MyHomePage object that was created by
83
+ // the App.build method, and use it to set our appbar title.
45
84
title: Text (widget.title),
46
85
),
47
86
body: Center (
87
+ // Center is a layout widget. It takes a single child and positions it
88
+ // in the middle of the parent.
48
89
child: Column (
90
+ // Column is also a layout widget. It takes a list of children and
91
+ // arranges them vertically. By default, it sizes itself to fit its
92
+ // children horizontally, and tries to be as tall as its parent.
93
+ //
94
+ // Invoke "debug painting" (press "p" in the console, choose the
95
+ // "Toggle Debug Paint" action from the Flutter Inspector in Android
96
+ // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
97
+ // to see the wireframe for each widget.
98
+ //
99
+ // Column has various properties to control how it sizes itself and
100
+ // how it positions its children. Here we use mainAxisAlignment to
101
+ // center the children vertically; the main axis here is the vertical
102
+ // axis because Columns are vertical (the cross axis would be
103
+ // horizontal).
49
104
mainAxisAlignment: MainAxisAlignment .center,
50
105
children: < Widget > [
106
+ const Text (
107
+ 'You have pushed the button this many times:' ,
108
+ ),
109
+ Text (
110
+ '$_counter ' ,
111
+ style: Theme .of (context).textTheme.headlineMedium,
112
+ ),
51
113
Text (
52
114
'FLUTTER_BUILD_NAME: $_buildName ' ,
53
115
style: Theme .of (context).textTheme.bodyMedium,
@@ -59,6 +121,11 @@ class _MyHomePageState extends State<MyHomePage> {
59
121
],
60
122
),
61
123
),
124
+ floatingActionButton: FloatingActionButton (
125
+ onPressed: _incrementCounter,
126
+ tooltip: 'Increment' ,
127
+ child: const Icon (Icons .add),
128
+ ), // This trailing comma makes auto-formatting nicer for build methods.
62
129
);
63
130
}
64
131
}
0 commit comments