-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.dart
130 lines (122 loc) · 3.36 KB
/
calculator.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
import 'package:flutter/material.dart';
import 'package:math_expressions/math_expressions.dart';
// import 'practice.dart';
void main() {
runApp(CalculatorApp());
}
class CalculatorApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Simple Calculator',
theme: ThemeData(primarySwatch: Colors.blue),
home: CalculatorScreen(),
);
}
}
class CalculatorScreen extends StatefulWidget {
@override
_CalculatorScreenState createState() => _CalculatorScreenState();
}
class _CalculatorScreenState extends State<CalculatorScreen> {
String _expression = '';
void _onPressed(String buttonText) {
setState(() {
if (buttonText == 'C') {
_expression = '';
} else if (buttonText == '=') {
_evaluateExpression();
} else {
_expression += buttonText;
}
});
}
void _evaluateExpression() {
try {
Parser p = Parser();
Expression exp = p.parse(_expression);
ContextModel cm = ContextModel();
double eval = exp.evaluate(EvaluationType.REAL, cm);
_expression = eval.toString();
} catch (e) {
_expression = 'Error';
}
}
Widget _buildButton(String buttonText, {Color? textColor, Color? buttonColor}) {
return Expanded(
child: OutlinedButton(
onPressed: () => _onPressed(buttonText),
child: Text (
buttonText,
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold,
color: textColor ?? Colors.black,
),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(buttonColor ?? Colors.white),
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Simple Calculator'),
),
body: Column(
children: <Widget>[
Expanded(
child: Container(
alignment: Alignment.bottomRight,
padding: EdgeInsets.all(16.0),
child: Text(
_expression,
style: TextStyle(fontSize: 48.0, fontWeight: FontWeight.bold),
),
),
),
Divider(),
Row(
children: <Widget>[
_buildButton('7'),
_buildButton('8'),
_buildButton('9'),
_buildButton('/', buttonColor: Colors.orange),
],
),
SizedBox(height: 5),
Row(
children: <Widget>[
_buildButton('4'),
_buildButton('5'),
_buildButton('6'),
_buildButton('*', buttonColor: Colors.orange),
],
),
SizedBox(height: 5,),
Row(
children: <Widget>[
_buildButton('1'),
_buildButton('2'),
_buildButton('3'),
_buildButton('-', buttonColor: Colors.orange),
],
),
SizedBox(height: 5,),
Row(
children: <Widget>[
_buildButton('C', textColor: Colors.red),
_buildButton('0'),
_buildButton('=', buttonColor: Colors.blue),
_buildButton('+', buttonColor: Colors.orange),
],
),
],
),
);
}
}