import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var num1=0,num2=0,sum=0,numclear = 0;
final TextEditingController t1 = new TextEditingController(text: "0");
final TextEditingController t2 = new TextEditingController(text: "0");
void doAddition(){
setState(() {
num1 = int.parse(t1.text);
num2 = int.parse(t2.text);
sum = num1+num2;
});
}
void doSubtraction(){
setState(() {
num1 = int.parse(t1.text);
num2 = int.parse(t2.text);
sum = num1-num2;
});
}
void doMultiplication(){
setState(() {
num1 = int.parse(t1.text);
num2 = int.parse(t2.text);
sum = num1*num2;
});
}
void doDivision(){
setState(() {
num1 = int.parse(t1.text);
num2 = int.parse(t2.text);
sum = num1~/num2;
});
}
void doClear(){
setState(() {
t1.text ="0";
t2.text ="0";
sum = numclear;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text("Calculator"),
),
body: new Container(
padding: EdgeInsets.all(40.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget> [
new Text("Output : $sum",style: TextStyle(fontSize: 30,
color: Colors.purpleAccent,fontWeight: FontWeight.bold),),
new TextField(
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: "Enter Number 1"),
controller: t1,
),
new TextField(
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: "Enter Number 2"),
controller: t2,
),
new Padding(
padding: EdgeInsets.only(top: 20),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new MaterialButton(
child: Text("+",style: TextStyle(fontSize: 20),),
onPressed: doAddition,
color: Colors.greenAccent,
),new MaterialButton(
child: Text("-",style: TextStyle(fontSize: 30),),
onPressed: doSubtraction,
color: Colors.greenAccent,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new MaterialButton(
child: Text("X",style: TextStyle(fontSize: 20),),
onPressed: doMultiplication,
color: Colors.greenAccent,
),new MaterialButton(
child: Text("/",style: TextStyle(fontSize: 20),),
onPressed: doDivision,
color: Colors.greenAccent,
),
],
),
new Padding(
padding: EdgeInsets.only(top: 40),
),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
new MaterialButton(
onPressed: doClear,
child: Icon(Icons.refresh),
color: Colors.greenAccent,
)
],
)
],
)
),
);
}
}