calc update

This commit is contained in:
2025-04-15 09:27:35 +02:00
parent 0bf57b22d3
commit ed46f28560

View File

@@ -18,38 +18,55 @@ class _SimpleCalcState extends State<SimpleCalc> {
// Array of button // Array of button
final List<String> buttons = [ final List<String> buttons = [
'C', 'AC',
'(', '(',
')', ')',
'D', '÷',
'7', '7',
'8', '8',
'9', '9',
'/', 'x',
'4', '4',
'5', '5',
'6', '6',
'x', '-',
'1', '1',
'2', '2',
'3', '3',
'-', '+',
'0', '0',
'.', '.',
'D',
'=', '=',
'+',
]; ];
// function to calculate the input operation // function to calculate the input operation
void equalPressed() { void equalPressed() {
String finaluserinput = userInput; String finaluserinput = userInput;
finaluserinput = userInput.replaceAll('x', '*'); finaluserinput = finaluserinput.replaceAll('x', '*');
finaluserinput = finaluserinput.replaceAll('÷', '/');
print(finaluserinput);
Parser p = Parser(); Parser p = Parser();
Expression exp = p.parse(finaluserinput); Expression exp = p.parse(finaluserinput);
ContextModel cm = ContextModel(); ContextModel cm = ContextModel();
double eval = exp.evaluate(EvaluationType.REAL, cm); double eval = exp.evaluate(EvaluationType.REAL, cm);
answer = eval.toString(); if (eval.toString().length <= 1) {
} else if (eval
.toString()
.substring(eval.toString().length - 2, eval.toString().length) ==
".0") {
answer = eval.toString().substring(0, eval.toString().length - 2);
} else {
answer = eval.toString();
}
}
bool isNumeric(String? s) {
if (s == null) {
return false;
}
return double.tryParse(s) != null;
} }
@override @override
@@ -61,13 +78,13 @@ class _SimpleCalcState extends State<SimpleCalc> {
} }
Widget getBody() { Widget getBody() {
double width = MediaQuery.sizeOf(context).width; // double width = MediaQuery.sizeOf(context).width;
double height = MediaQuery.sizeOf(context).height; double height = MediaQuery.sizeOf(context).height;
var padding = MediaQuery.paddingOf(context); // var padding = MediaQuery.paddingOf(context);
double newheight = height - padding.top - padding.bottom; // double newheight = height - padding.top - padding.bottom;
print("width: $width"); // print("width: $width");
print("height: $height"); // print("height: $height");
print("newheight: $newheight"); // print("newheight: $newheight");
double calcWidth = 500; double calcWidth = 500;
if (MzanziInnovationHub.of(context)!.theme.screenType == "desktop") { if (MzanziInnovationHub.of(context)!.theme.screenType == "desktop") {
if (height < 700) { if (height < 700) {
@@ -92,14 +109,18 @@ class _SimpleCalcState extends State<SimpleCalc> {
color: MzanziInnovationHub.of(context)!.theme.secondaryColor()), color: MzanziInnovationHub.of(context)!.theme.secondaryColor()),
const SizedBox(height: 10), const SizedBox(height: 10),
Container( Container(
//color: Colors.white,
padding: const EdgeInsets.all(20), padding: const EdgeInsets.all(20),
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
child: Text( child: SingleChildScrollView(
userInput, scrollDirection: Axis.horizontal,
style: TextStyle( reverse: true,
fontSize: 18, child: Text(
color: MzanziInnovationHub.of(context)!.theme.secondaryColor(), userInput,
style: TextStyle(
fontSize: 40,
color:
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
),
), ),
), ),
), ),
@@ -149,9 +170,7 @@ class _SimpleCalcState extends State<SimpleCalc> {
}); });
}, },
buttonText: buttons[index], buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)! buttonColor: Color.fromRGBO(214, 171, 255, 1),
.theme
.messageTextColor(),
textColor: MzanziInnovationHub.of(context)! textColor: MzanziInnovationHub.of(context)!
.theme .theme
.primaryColor(), .primaryColor(),
@@ -159,7 +178,7 @@ class _SimpleCalcState extends State<SimpleCalc> {
); );
} }
// +/- button // ( button
else if (index == 1) { else if (index == 1) {
return Padding( return Padding(
padding: const EdgeInsets.all(4.0), padding: const EdgeInsets.all(4.0),
@@ -179,7 +198,7 @@ class _SimpleCalcState extends State<SimpleCalc> {
), ),
); );
} }
// % Button // ) Button
else if (index == 2) { else if (index == 2) {
return Padding( return Padding(
padding: const EdgeInsets.all(4.0), padding: const EdgeInsets.all(4.0),
@@ -199,51 +218,11 @@ class _SimpleCalcState extends State<SimpleCalc> {
), ),
); );
} }
// Delete Button
else if (index == 3) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput =
userInput.substring(0, userInput.length - 1);
});
},
buttonText: buttons[index],
buttonColor:
MzanziInnovationHub.of(context)!.theme.errorColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// Equal_to Button
else if (index == 18) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
equalPressed();
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.successColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// +, -, / x buttons // +, -, / x buttons
else if (index == 7 || else if (index == 3 ||
index == 7 ||
index == 11 || index == 11 ||
index == 15 || index == 15) {
index == 19) {
return Padding( return Padding(
padding: const EdgeInsets.all(4.0), padding: const EdgeInsets.all(4.0),
child: MIHButton( child: MIHButton(
@@ -254,8 +233,8 @@ class _SimpleCalcState extends State<SimpleCalc> {
}); });
} else { } else {
setState(() { setState(() {
userInput = answer; // userInput = answer;
answer = "0"; // answer = "0";
userInput += buttons[index]; userInput += buttons[index];
}); });
} }
@@ -273,6 +252,59 @@ class _SimpleCalcState extends State<SimpleCalc> {
), ),
); );
} }
// delete Button
else if (index == 18) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
if (userInput.length == 1) {
userInput = '0';
} else if (userInput.length > 1) {
userInput =
userInput.substring(0, userInput.length - 1);
}
if (!isNumeric(userInput[userInput.length - 1])) {
userInput =
userInput.substring(0, userInput.length - 1);
}
// userInput =
// userInput.substring(0, userInput.length - 1);
equalPressed();
});
},
buttonText: buttons[index],
buttonColor:
MzanziInnovationHub.of(context)!.theme.errorColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// Equal_to Button
else if (index == 19) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
equalPressed();
userInput = answer;
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.successColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// other buttons // other buttons
else { else {
return Padding( return Padding(
@@ -281,6 +313,7 @@ class _SimpleCalcState extends State<SimpleCalc> {
onTap: () { onTap: () {
setState(() { setState(() {
userInput += buttons[index]; userInput += buttons[index];
equalPressed();
}); });
}, },
buttonText: buttons[index], buttonText: buttons[index],