Skip to content

Instantly share code, notes, and snippets.

@vivekascoder
Created October 6, 2020 10:08
Show Gist options
  • Select an option

  • Save vivekascoder/4492d098a9c06d8733d2e776ef3c35e4 to your computer and use it in GitHub Desktop.

Select an option

Save vivekascoder/4492d098a9c06d8733d2e776ef3c35e4 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var x, y, sum;
final TextEditingController a = new TextEditingController(text: '0');
final TextEditingController b = new TextEditingController(text: '0');
void _sum(){
setState(() {
x = int.parse(a.text);
y = int.parse(b.text);
sum = x + y;
});
}
void _calculate(String operation){
if (operation == "+"){
setState(() {
x = int.parse(a.text);
y = int.parse(b.text);
sum = x + y;
});
} else if(operation == "-"){
setState(() {
x = int.parse(a.text);
y = int.parse(b.text);
sum = x - y;
});
} else if(operation == "*"){
setState(() {
x = int.parse(a.text);
y = int.parse(b.text);
sum = x * y;
});
} else if(operation == "/"){
setState(() {
x = int.parse(a.text);
y = int.parse(b.text);
sum = x / y;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text("CalCI"),
),
body: new Container(
padding: EdgeInsets.all(40),
child: new Center(
child: new Column(
children: <Widget>[
new Text(
"Output: " + this.sum.toString(),
style: new TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
new TextField(
keyboardType: TextInputType.number,
controller: a,
decoration: new InputDecoration(
hintText: "Enter No:",
labelText: "Enter first number"
),
),
new TextField(
keyboardType: TextInputType.number,
controller: b,
decoration: new InputDecoration(
hintText: "Enter No:",
labelText: "Enter second number"
),
),
new Padding(padding: EdgeInsets.only(
top: 50
)),
new Row(
children: <Widget>[
new RaisedButton(
onPressed: ()=> _calculate('+'),
child: new Text("+"),
),
new RaisedButton(
onPressed: ()=> _calculate('-'),
child: new Text("-"),
),
],
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
),
new Row(
children: <Widget>[
new RaisedButton(
onPressed: ()=> _calculate('*'),
child: new Text("*"),
),
new RaisedButton(
onPressed: ()=> _calculate('/'),
child: new Text("/"),
),
],
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
)
],
mainAxisAlignment: MainAxisAlignment.center,
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment