Calculator Application Simple with StatefulWidget

 import "package:flutter/material.dart";


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

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Hello",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.lightGreen,
),
home: const FlutterApp(),
);
}
}

class FlutterApp extends StatefulWidget {
const FlutterApp({super.key});

@override
State<StatefulWidget> createState() {
return FlutterAppState();
}
}

class FlutterAppState extends State<FlutterApp> {
var no1Controller = TextEditingController();
var no2Controller = TextEditingController();

var result = " ";

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusManager.instance.primaryFocus?.unfocus();
},
child: Scaffold(
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const UserAccountsDrawerHeader(
accountName: Text("John"),
accountEmail: Text("John@google.com"),
currentAccountPicture: Icon(Icons.account_circle, size: 85),
),
ListTile(
leading: const Icon(Icons.message),
title: const Text("message"),
onTap: () {
Navigator.pop(context);
},
),
const Divider(thickness: 1, color: Colors.black),
ListTile(
leading: const Icon(Icons.account_circle),
title: const Text("profile"),
onTap: () {
Navigator.pop(context);
},
),
const Divider(thickness: 1, color: Colors.black),
ListTile(
leading: const Icon(Icons.settings),
title: const Text("Settings"),
onTap: () {
Navigator.pop(context);
},
),
const Divider(thickness: 1, color: Colors.black),
ListTile(
leading: const Icon(Icons.help_outline),
title: const Text("Help & feedback"),
onTap: () {
Navigator.pop(context);
},
),
const Divider(thickness: 1, color: Colors.black),
ListTile(
leading: const Icon(Icons.restore_from_trash),
title: const Text("Trash"),
onTap: () {
Navigator.pop(context);
},
),
],
),
),
appBar: AppBar(
title: const Text(
"Flutter Calculator",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Container(
color: Colors.white,
width: 300.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
keyboardType: TextInputType.number,
controller: no1Controller,
decoration: InputDecoration(
hintText: "Enter the First Value...",
labelText: "First Value",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(11.0),
),
),
),
const SizedBox(height: 30.0),
TextField(
keyboardType: TextInputType.number,
controller: no2Controller,
decoration: InputDecoration(
hintText: "Enter the Second Value...",
labelText: "Second Value",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(11.0),
),
),
),
const SizedBox(height: 50.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
var no1 = int.parse(no1Controller.text);
var no2 = int.parse(no2Controller.text);

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

/*A string can be cast to an integer using the int.parse() method in Dart.
The method takes a string as an argument and converts it into an integer*/

var sum = no1 + no2;

result = "The sum of $no1 and $no2 = $sum";

setState(
() {},
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Add",
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
),
),
//SizedBox(width: 10,),

ElevatedButton(
onPressed: () {
var no1 = int.parse(no1Controller.text.toString());
var no2 = int.parse(no2Controller.text.toString());

var sub = no1 - no2;

result = "The subtraction of $no1 and $no2 = $sub";

setState(
() {},
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"sub",
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
),
),
//SizedBox(width: 10,),

ElevatedButton(
onPressed: () {
var no1 = int.parse(no1Controller.text.toString());
var no2 = int.parse(no2Controller.text.toString());

var mul = no1 * no2;

result = "The Multiplication of $no1 and $no2 = $mul";

setState(
() {},
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Mul",
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
),
),
//SizedBox(width: 10,),

ElevatedButton(
onPressed: () {
var no1 = int.parse(no1Controller.text.toString());
var no2 = int.parse(no2Controller.text.toString());

var div = no1 / no2;

result = "The Division of $no1 and $no2 = $div";
/*To limit the digits after point below technique is used*/
/*result = "The Division of $no1 and $no2 = ${div.toStringAsFixed(4)}";*/

setState(
() {},
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Div",
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
),
),
],
),
Padding(
padding: const EdgeInsets.all(21),
child: Text(
result,
style: const TextStyle(fontSize: 25, color: Colors.black),
),
),
],
),
),
),
),
);
}
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter