Simple Calculator with Flutter
Built Simple Calculator with Flutter a simple user interface
Let's begin
main.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main()
{
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
theme: ThemeData(
primaryColor: Colors.deepOrange
),
),
);
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int firstnum,secnum;
String textdisplay="";
String result;
String whichOperatorSelected;
btnClicked(String btnval)
{
if(btnval=="c")
{
textdisplay="";
result="";
firstnum=0;
secnum=0;
}
else if(btnval=="+" || btnval=="-" ||btnval=="/" || btnval=="x")
{
firstnum=int.parse(textdisplay);
result="";
whichOperatorSelected=btnval; //Set Which Operator chose
}
else if(btnval=="=")
{
secnum=int.parse(textdisplay);
if(whichOperatorSelected=="+")
{
result=(firstnum + secnum).toString();
}
if(whichOperatorSelected=="-")
{
result=(firstnum - secnum).toString();
}
if(whichOperatorSelected=="x")
{
result=(firstnum * secnum).toString();
}
if(whichOperatorSelected=="/")
{
result=(firstnum / secnum).toString();
}
}
else{
result=int.parse(textdisplay + btnval).toString();
}
setState(() {
textdisplay=result;
});
}
Widget customeButton(String btnval){
return Expanded(
child: OutlineButton(
highlightElevation: 4,
splashColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(10.0)
),
onPressed: () => btnClicked(btnval),
padding: EdgeInsets.all(25.0),
child: Text(btnval,style: TextStyle(
fontSize: 25.0,
),),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Calculator"),
),
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Expanded(
child: Container(
padding: EdgeInsets.all(15.0),
alignment: Alignment.bottomRight,
child: Text(
"$textdisplay",
style: TextStyle(
fontSize: 40.0,
fontWeight: FontWeight.w600,
),),
),
),
Row(
children: <Widget>[
customeButton("7"),
customeButton("8"),
customeButton("9"),
customeButton("+"),
],
),
Row(
children: <Widget>[
customeButton("4"),
customeButton("5"),
customeButton("6"),
customeButton("-"),
],
),
Row(
children: <Widget>[
customeButton("1"),
customeButton("2"),
customeButton("3"),
customeButton("x"),
],
),
Row(
children: <Widget>[
customeButton("c"),
customeButton("0"),
customeButton("="),
customeButton("/"),
],
),
],
),
),
);
}
}
Let's begin
main.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main()
{
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
theme: ThemeData(
primaryColor: Colors.deepOrange
),
),
);
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int firstnum,secnum;
String textdisplay="";
String result;
String whichOperatorSelected;
btnClicked(String btnval)
{
if(btnval=="c")
{
textdisplay="";
result="";
firstnum=0;
secnum=0;
}
else if(btnval=="+" || btnval=="-" ||btnval=="/" || btnval=="x")
{
firstnum=int.parse(textdisplay);
result="";
whichOperatorSelected=btnval; //Set Which Operator chose
}
else if(btnval=="=")
{
secnum=int.parse(textdisplay);
if(whichOperatorSelected=="+")
{
result=(firstnum + secnum).toString();
}
if(whichOperatorSelected=="-")
{
result=(firstnum - secnum).toString();
}
if(whichOperatorSelected=="x")
{
result=(firstnum * secnum).toString();
}
if(whichOperatorSelected=="/")
{
result=(firstnum / secnum).toString();
}
}
else{
result=int.parse(textdisplay + btnval).toString();
}
setState(() {
textdisplay=result;
});
}
Widget customeButton(String btnval){
return Expanded(
child: OutlineButton(
highlightElevation: 4,
splashColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(10.0)
),
onPressed: () => btnClicked(btnval),
padding: EdgeInsets.all(25.0),
child: Text(btnval,style: TextStyle(
fontSize: 25.0,
),),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Calculator"),
),
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Expanded(
child: Container(
padding: EdgeInsets.all(15.0),
alignment: Alignment.bottomRight,
child: Text(
"$textdisplay",
style: TextStyle(
fontSize: 40.0,
fontWeight: FontWeight.w600,
),),
),
),
Row(
children: <Widget>[
customeButton("7"),
customeButton("8"),
customeButton("9"),
customeButton("+"),
],
),
Row(
children: <Widget>[
customeButton("4"),
customeButton("5"),
customeButton("6"),
customeButton("-"),
],
),
Row(
children: <Widget>[
customeButton("1"),
customeButton("2"),
customeButton("3"),
customeButton("x"),
],
),
Row(
children: <Widget>[
customeButton("c"),
customeButton("0"),
customeButton("="),
customeButton("/"),
],
),
],
),
),
);
}
}
