-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomapage.dart
143 lines (136 loc) · 4.71 KB
/
homapage.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import 'package:calculator/buttons.dart';
import 'package:flutter/material.dart';
import 'package:math_expressions/math_expressions.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var userQuestion = '';
var userAnswer = '';
final mytextstyle = TextStyle(fontSize: 20, fontWeight: FontWeight.bold);
final List<String> buttons = [
'AC',
'DEL',
'%',
'/',
'7',
'8',
'9',
'X',
'4',
'5',
'6',
'-',
'1',
'2',
'3',
'+',
'0',
'.',
'ANC',
'=',
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.deepPurple[50],
body: Column(
children: [
Expanded(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SizedBox(
height: 50,
),
Container(
padding: EdgeInsets.all(20),
alignment: Alignment.centerLeft,
child: Text(
userQuestion,
style: TextStyle(fontSize: 20),
),
),
Container(
padding: EdgeInsets.all(20),
alignment: Alignment.centerRight,
child: Text(
userAnswer,
style: TextStyle(fontSize: 20),
),
)
],
),
)),
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.all(5),
child: Container(
child: Center(
child: GridView.builder(
itemCount: buttons.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4),
itemBuilder: (BuildContext context, int index) {
if (index == 0) {
return MyButtons(
buttonTapped: () {
setState(() {
userQuestion = '';
});
},
buttontext: buttons[index],
color: Colors.green,
textColor: Colors.white,
);
} else if (index == buttons.length - 1) {
return MyButtons(
buttonTapped: () {
setState(() {
equalpressed();
});
},
buttontext: buttons[index],
color: Colors.red,
textColor: Colors.white,
);
} else {
return MyButtons(
buttonTapped: () {
setState(() {
userQuestion += buttons[index];
});
},
buttontext: buttons[index],
color: isoperator(buttons[index])
? Colors.deepPurple
: Colors.black,
textColor: Colors.white,
);
}
})),
)))
],
),
);
}
bool isoperator(String x) {
if (x == '%' || x == '/' || x == 'X' || x == '-' || x == '+' || x == '=') {
return true;
}
return false;
}
void equalpressed() {
String finalQuestion = userQuestion;
finalQuestion = finalQuestion.replaceAll('X', '*');
Parser p = Parser();
Expression exp = p.parse(finalQuestion);
ContextModel cm = ContextModel();
double eval = exp.evaluate(EvaluationType.REAL, cm);
userAnswer = eval.toString();
}
}