1
1
import 'package:flutter/material.dart' ;
2
2
3
3
void main (){
4
- runApp (calculator ());
4
+ runApp (Calculator ());
5
5
}
6
6
7
- class calculator extends StatelessWidget {
7
+ class Calculator extends StatelessWidget {
8
8
@override
9
9
Widget build (BuildContext context) {
10
10
@@ -23,7 +23,42 @@ class SimpleCalculator extends StatefulWidget {
23
23
24
24
class _SimpleCalculatorState extends State <SimpleCalculator > {
25
25
26
- Widget BuildButton (String buttonText, double buttonHeight, Color buttonColor){
26
+ String equation = "0" ;
27
+ String result = "0" ;
28
+ String expression = "" ;
29
+ double equationFontSize = 38.0 ;
30
+ double resultFontSize = 48.0 ;
31
+
32
+ buttonPressed (String buttonText){
33
+ setState (() {
34
+ if (buttonText == "C" ){
35
+ equation = "0" ;
36
+ result = "0" ;
37
+ equationFontSize = 38.0 ;
38
+ resultFontSize = 48.0 ;
39
+ }else if (buttonText == "⌫" ){
40
+ equationFontSize = 48.0 ;
41
+ resultFontSize = 38.0 ;
42
+ equation = equation.substring (0 , equation.length - 1 );
43
+ if (equation == "" ){
44
+ equation = "0" ;
45
+ }
46
+ }else if (buttonText == "=" ){
47
+ equationFontSize = 38.0 ;
48
+ resultFontSize = 48.0 ;
49
+ }else {
50
+ equationFontSize = 48.0 ;
51
+ resultFontSize = 38.0 ;
52
+ if (equation == "0" ){
53
+ equation = buttonText;
54
+ }else {
55
+ equation = equation + buttonText;
56
+ }
57
+ }
58
+ });
59
+ }
60
+
61
+ Widget buildButton (String buttonText, double buttonHeight, Color buttonColor){
27
62
return Container (
28
63
height: MediaQuery .of (context).size.height * 0.1 * buttonHeight,
29
64
color: buttonColor,
@@ -37,7 +72,7 @@ class _SimpleCalculatorState extends State<SimpleCalculator> {
37
72
)
38
73
),
39
74
padding: EdgeInsets .all (16.0 ),
40
- onPressed: null ,
75
+ onPressed: () => buttonPressed (buttonText) ,
41
76
child: Text (
42
77
buttonText,
43
78
style: TextStyle (
@@ -59,12 +94,12 @@ class _SimpleCalculatorState extends State<SimpleCalculator> {
59
94
Container (
60
95
alignment: Alignment .centerRight,
61
96
padding: EdgeInsets .fromLTRB (10 , 20 , 20 , 0 ),
62
- child: Text ("0" , style: TextStyle (fontSize: 38.0 ),),
97
+ child: Text (equation , style: TextStyle (fontSize: equationFontSize ),),
63
98
),
64
99
Container (
65
100
alignment: Alignment .centerRight,
66
101
padding: EdgeInsets .fromLTRB (10 , 30 , 20 , 0 ),
67
- child: Text ("0" , style: TextStyle (fontSize: 48.0 ),),
102
+ child: Text (result , style: TextStyle (fontSize: resultFontSize ),),
68
103
),
69
104
Expanded (
70
105
child: Divider (),
@@ -78,12 +113,69 @@ class _SimpleCalculatorState extends State<SimpleCalculator> {
78
113
children: [
79
114
TableRow (
80
115
children: [
81
- BuildButton ("C" , 1 , Colors .redAccent),
116
+ buildButton ("C" , 1 , Colors .redAccent),
117
+ buildButton ("⌫" , 1 , Colors .blue),
118
+ buildButton ("÷" , 1 , Colors .blue),
82
119
]
83
- )
120
+ ),
121
+ TableRow (
122
+ children: [
123
+ buildButton ("7" , 1 , Colors .black45),
124
+ buildButton ("8" , 1 , Colors .black45),
125
+ buildButton ("9" , 1 , Colors .black45),
126
+ ]
127
+ ),
128
+ TableRow (
129
+ children: [
130
+ buildButton ("4" , 1 , Colors .black45),
131
+ buildButton ("5" , 1 , Colors .black45),
132
+ buildButton ("6" , 1 , Colors .black45),
133
+ ]
134
+ ),
135
+ TableRow (
136
+ children: [
137
+ buildButton ("1" , 1 , Colors .black45),
138
+ buildButton ("2" , 1 , Colors .black45),
139
+ buildButton ("3" , 1 , Colors .black45),
140
+ ]
141
+ ),
142
+ TableRow (
143
+ children: [
144
+ buildButton ("." , 1 , Colors .black45),
145
+ buildButton ("0" , 1 , Colors .black45),
146
+ buildButton ("00" , 1 , Colors .black45),
147
+ ]
148
+ ),
84
149
],
85
150
)
86
151
,
152
+ ),
153
+ Container (
154
+ width: MediaQuery .of (context).size.width * 0.25 ,
155
+ child: Table (
156
+ children: [
157
+ TableRow (
158
+ children: [
159
+ buildButton ("×" , 1 , Colors .blue),
160
+ ]
161
+ ),
162
+ TableRow (
163
+ children: [
164
+ buildButton ("+" , 1 , Colors .blue),
165
+ ]
166
+ ),
167
+ TableRow (
168
+ children: [
169
+ buildButton ("-" , 1 , Colors .blue),
170
+ ]
171
+ ),
172
+ TableRow (
173
+ children: [
174
+ buildButton ("=" , 2 , Colors .redAccent),
175
+ ]
176
+ )
177
+ ],
178
+ ),
87
179
)
88
180
],
89
181
),
0 commit comments