making basic calculator in flutter

 import 'package:flutter/material.dart';

import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import 'package:intl/intl.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
textTheme: TextTheme(
headline1: TextStyle(
fontWeight: FontWeight.bold, fontSize: 25, color: Colors.black),
subtitle1: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.green)),
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return MyHomeState();
}

}

class MyHomeState extends State<MyHomePage>{
var no1Controller = TextEditingController();
var no2Controller = TextEditingController();

var result = "";

var count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stateful widget'),
),

body: Container(
color: Colors.blue.shade200,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
keyboardType: TextInputType.number,
controller: no1Controller,
),
TextField(
keyboardType: TextInputType.number,
controller: no2Controller,
),
Padding(
padding: const EdgeInsets.all(21.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(onPressed: (){
var no1 = int.parse(no1Controller.text.toString());
var no2 = int.parse(no2Controller.text.toString());
var sum = no1+no2;

result = " The sum of $no1 and $no2 is $sum";
setState(() {
});
}, child: Text('Add')),
ElevatedButton(onPressed: (){

var no1 = int.parse(no1Controller.text.toString());
var no2 = int.parse(no2Controller.text.toString());
var Difference = no1-no2;

result = " The difference of $no1 and $no2 is $Difference";
setState(() {
});
}, child: Text('Subtract')),
ElevatedButton(onPressed: (){

var no1 = int.parse(no1Controller.text.toString());
var no2 = int.parse(no2Controller.text.toString());
var product = no1*no2;

result = " The product of $no1 and $no2 is $product";
setState(() {
});
}, child: Text('Multiply')),
ElevatedButton(onPressed: (){

var no1 = int.parse(no1Controller.text.toString());
var no2 = int.parse(no2Controller.text.toString());
var quotient = no1/no2;

result = "The $no1 can be divided by $no2, ${quotient.toStringAsFixed(2)} times";
setState(() {
});
}, child: Text('Divide')),
],
),
),
Padding(padding: const EdgeInsets.all(21),
child: Text(result, style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold),),)
],
),
),
),
));
}

}


Comments

Popular posts from this blog

swapping the alternate values in given array's element ! 17/11/2022

Learning stage | c++ programs

c++ basic question