Created
August 9, 2021 22:33
-
-
Save IsmailAlamKhan/439b35c5b42ba9f5be785c11b15e2496 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// import 'calcs.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
bool operand = false; | |
String result = " "; | |
final ButtonStyle style = ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)); | |
@override | |
Widget build(BuildContext context) { | |
var padding = MediaQuery.of(context).padding; | |
double width = MediaQuery.of(context).size.width; | |
double height= MediaQuery.of(context).size.height - padding.top - padding.bottom; | |
return Scaffold( | |
appBar: AppBar(title: Text("Calculator")), | |
body: Center( | |
child: Column( | |
children: <Widget>[ | |
Row( | |
children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.fromLTRB(20.0, 50.0, 0.0, 50.0), | |
child: Text(result, style: TextStyle(fontSize: 25.0)), | |
), | |
], | |
), | |
Row( children: [ | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("1"), child: Text("1"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("2"), child: Text("2"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("3"), child: Text("3"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("4"), child: Text("4"), style: style), | |
), | |
]), | |
Row( children: [ | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("5"), child: Text("5"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("6"), child: Text("6"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("7"), child: Text("7"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> deleteLast(), child: Text("<-"), style: style), | |
), | |
]), | |
Row( children: [ | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("8"), child: Text("8"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("0"), child: Text("9"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult("0"), child: Text("0"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> finalResult(Calculations().calculations(result)), child: Text("="), style: style), | |
), | |
]), | |
Row( children: [ | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult(" + "), child: Text("+"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult(" - "), child: Text("-"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult(" * "), child: Text("*"), style: style), | |
), | |
SizedBox( | |
height: width/4, | |
width: width/4, | |
child: ElevatedButton(onPressed: ()=> changeResult(" / "), child: Text("/"), style: style), | |
), | |
]), | |
], | |
), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
void deleteLast() { | |
if ((result != null) && (result.length > 0)) { | |
setState(() { | |
if(result[result.length-1] == " ") { | |
result = result.substring(0, result.length - 3); | |
operand = false; | |
} else | |
result = result.substring(0, result.length - 1); | |
}); | |
} | |
} | |
void changeResult(String s) { | |
setState(() { | |
if (s == " + " || s == " - " || s == " * " || s == " / ") { | |
if (operand == false) { | |
result += s; | |
operand = true; | |
} | |
} else | |
result += s; | |
}); | |
} | |
void finalResult(String s) { | |
print("A"); | |
setState(() { | |
result = s; | |
}); | |
} | |
} | |
class Calculations { | |
String calculations(String s) { | |
int finalNumber = 0; | |
int num1 = firstNumber(s); | |
int num2 = secondNumber(s); | |
for (int i = 0; i < s.length; i++) { | |
if (s[i] == "+") | |
return (num1 + num2).toString(); | |
else if (s[i] == "-") | |
return (num1 - num2).toString(); | |
else if (s[i] == "*") | |
return (num1 * num2).toString(); | |
else if (s[i] == "/") | |
return (num1 / num2).toString(); | |
} | |
return finalNumber.toString(); | |
} | |
int firstNumber(String s) { | |
int num1 = 0; | |
for (int i = 0; i < s.length; i++) { | |
if (s[i] == " ") | |
break; | |
num1 = num1 * 10 + int.parse(s[i]); | |
} | |
return num1; | |
} | |
int secondNumber(String s) { | |
int num2 = 0; | |
bool start = false; | |
for (int i = 0; i < s.length; i++) { | |
if (s[i] == "+" || s[i] == "-" || s[i] == "*" || s[i] == "/") | |
start = true; | |
if (s[i] == " ") | |
continue; | |
num2 = num2 * 10 + int.parse(s[i]); | |
} | |
return num2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment