Skip to content

Commit acf7e35

Browse files
committed
numbers, clear and back button func add
1 parent fd5a622 commit acf7e35

File tree

1 file changed

+100
-8
lines changed

1 file changed

+100
-8
lines changed

lib/main.dart

+100-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'package:flutter/material.dart';
22

33
void main(){
4-
runApp(calculator());
4+
runApp(Calculator());
55
}
66

7-
class calculator extends StatelessWidget {
7+
class Calculator extends StatelessWidget {
88
@override
99
Widget build(BuildContext context) {
1010

@@ -23,7 +23,42 @@ class SimpleCalculator extends StatefulWidget {
2323

2424
class _SimpleCalculatorState extends State<SimpleCalculator> {
2525

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){
2762
return Container(
2863
height: MediaQuery.of(context).size.height * 0.1 * buttonHeight,
2964
color: buttonColor,
@@ -37,7 +72,7 @@ class _SimpleCalculatorState extends State<SimpleCalculator> {
3772
)
3873
),
3974
padding: EdgeInsets.all(16.0),
40-
onPressed: null,
75+
onPressed: () => buttonPressed(buttonText),
4176
child: Text(
4277
buttonText,
4378
style: TextStyle(
@@ -59,12 +94,12 @@ class _SimpleCalculatorState extends State<SimpleCalculator> {
5994
Container(
6095
alignment: Alignment.centerRight,
6196
padding: EdgeInsets.fromLTRB(10, 20, 20, 0),
62-
child: Text("0", style: TextStyle(fontSize: 38.0),),
97+
child: Text(equation, style: TextStyle(fontSize: equationFontSize),),
6398
),
6499
Container(
65100
alignment: Alignment.centerRight,
66101
padding: EdgeInsets.fromLTRB(10, 30, 20, 0),
67-
child: Text("0", style: TextStyle(fontSize: 48.0),),
102+
child: Text(result, style: TextStyle(fontSize: resultFontSize),),
68103
),
69104
Expanded(
70105
child: Divider(),
@@ -78,12 +113,69 @@ class _SimpleCalculatorState extends State<SimpleCalculator> {
78113
children: [
79114
TableRow(
80115
children: [
81-
BuildButton("C", 1, Colors.redAccent),
116+
buildButton("C", 1, Colors.redAccent),
117+
buildButton("⌫", 1, Colors.blue),
118+
buildButton("÷", 1, Colors.blue),
82119
]
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+
),
84149
],
85150
)
86151
,
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+
),
87179
)
88180
],
89181
),

0 commit comments

Comments
 (0)