a basic calculator with flutter

 import 'package:flutter/material.dart';


void main() => runApp(CalculatorApp());

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

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Minhaj Calculator',
theme: ThemeData.dark().copyWith(
primaryColor: Colors.deepPurple,
scaffoldBackgroundColor: Colors.black,
),
home: Calculator(),
);
}
}

class Calculator extends StatefulWidget {
@override
_CalculatorState createState() => _CalculatorState();
}

class _CalculatorState extends State<Calculator> {
String _output = "0";
String _currentNumber = "";
String _operation = "";
double _num1 = 0;
double _num2 = 0;

void _buttonPressed(String buttonText) {
setState(() {
if (buttonText == "C") {
_output = "0";
_currentNumber = "";
_operation = "";
_num1 = 0;
_num2 = 0;
} else if (buttonText == "+" ||
buttonText == "-" ||
buttonText == "×" ||
buttonText == "÷") {
_num1 = double.parse(_output);
_operation = buttonText;
_currentNumber = "";
} else if (buttonText == "=") {
_num2 = double.parse(_currentNumber);
switch (_operation) {
case "+":
_output = (_num1 + _num2).toString();
break;
case "-":
_output = (_num1 - _num2).toString();
break;
case "×":
_output = (_num1 * _num2).toString();
break;
case "÷":
_output = (_num1 / _num2).toString();
break;
}
_currentNumber = _output;
} else {
_currentNumber += buttonText;
_output = _currentNumber;
}
});
}

Widget _buildButton(String buttonText, Color buttonColor) {
return Expanded(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: buttonColor,
padding: const EdgeInsets.all(24.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
),
child: Text(
buttonText,
style: const TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
),
onPressed: () => _buttonPressed(buttonText),
),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Minhaj Calculator'),
centerTitle: true,
),
body: Column(
children: <Widget>[
Container(
alignment: Alignment.centerRight,
padding:
const EdgeInsets.symmetric(vertical: 24.0, horizontal: 12.0),
child: Text(
_output,
style: const TextStyle(
fontSize: 48.0,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
const Expanded(
child: Divider(),
),
Column(
children: [
Row(
children: [
_buildButton("7", Colors.grey[800]!),
_buildButton("8", Colors.grey[800]!),
_buildButton("9", Colors.grey[800]!),
_buildButton("÷", Colors.deepPurple),
],
),
Row(
children: [
_buildButton("4", Colors.grey[800]!),
_buildButton("5", Colors.grey[800]!),
_buildButton("6", Colors.grey[800]!),
_buildButton("×", Colors.deepPurple),
],
),
Row(
children: [
_buildButton("1", Colors.grey[800]!),
_buildButton("2", Colors.grey[800]!),
_buildButton("3", Colors.grey[800]!),
_buildButton("-", Colors.deepPurple),
],
),
Row(
children: [
_buildButton("C", Colors.red),
_buildButton("0", Colors.grey[800]!),
_buildButton("=", Colors.deepPurple),
_buildButton("+", Colors.deepPurple),
],
),
],
),
],
),
);
}
}

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