forked from md-siam/package_of_the_day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_expressions.dart
95 lines (87 loc) · 2.82 KB
/
math_expressions.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
import 'package:flutter/material.dart';
import 'package:math_expressions/math_expressions.dart';
class MyMathExpressions extends StatefulWidget {
const MyMathExpressions({Key? key}) : super(key: key);
@override
State<MyMathExpressions> createState() => _MyMathExpressionsState();
}
class _MyMathExpressionsState extends State<MyMathExpressions> {
// use this controller to get what the user typed
final _textController = TextEditingController();
var userInput = '';
var answer = '0';
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.deepPurple[100],
appBar: AppBar(title: const Text('Math Expressions')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(20.0),
child: TextField(
controller: _textController,
decoration: InputDecoration(
hintText: 'Input like: 1+2-4*3',
border: const OutlineInputBorder(),
suffixIcon: IconButton(
onPressed: () {
// clear whats currently in the TextField
_textController.clear();
},
icon: const Icon(Icons.clear),
),
),
),
),
const SizedBox(height: 100),
Text(
'= $answer',
style: const TextStyle(
fontSize: 20,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 100),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
setState(() {
answer = '0';
});
},
child: const Text("Clear"),
),
ElevatedButton(
onPressed: () {
setState(() {
userInput = _textController.text;
equalPressed();
});
},
child: const Text("Answer"),
),
],
),
],
),
),
);
}
void equalPressed() {
String finaluserinput = userInput;
finaluserinput = userInput.replaceAll('x', '*');
// '%' = Modulo operator
//finaluserinput = userInput.replaceAll('%', '*0.01');
Parser p = Parser();
Expression exp = p.parse(finaluserinput);
ContextModel cm = ContextModel();
double eval = exp.evaluate(EvaluationType.REAL, cm);
answer = eval.toString();
}
}