0% found this document useful (0 votes)
5 views8 pages

Calculator

The document is a student project from the Fergana branch of Tashkent Information Technologies University, focusing on mobile application development. It presents a Flutter-based calculator application, detailing its structure and functionality through Dart code. The application allows users to perform basic arithmetic operations with a user-friendly interface.

Uploaded by

usmonaliyevsaloh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Calculator

The document is a student project from the Fergana branch of Tashkent Information Technologies University, focusing on mobile application development. It presents a Flutter-based calculator application, detailing its structure and functionality through Dart code. The application allows users to perform basic arithmetic operations with a user-friendly interface.

Uploaded by

usmonaliyevsaloh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

O’ZBEKISTON RESPUBLIKASI RAQAMLI

TEXNOLOGIYALAR VAZIRLIGI

MUHAMMAD AL – XORAZMIY NOMIDAGI

TOSHKENT AXBOROT TEXNOLOGIYALARI


UNIVERSITETI

FARG’ONA FILIALI

“Kompyuter injiniring va sun’iy intellekt” fakulteti

Kompyuter injiniringi yo’nalishi

713-21 guruh talabasi

Rahmonov Akbarjonning

“Mobil ilovalarini ishlab chiqish”

fanidan tayyorlagan

MUSTAQIL ISHI
O’qituvchi: MUSAYEV X. SH.

main.dart​
;
import 'package:flutter/material.dart';

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

class IOSCalculatorApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: CalculatorPage(),
);
}
}

class CalculatorPage extends StatefulWidget {


@override
_CalculatorPageState createState() => _CalculatorPageState();
}

class _CalculatorPageState extends State<CalculatorPage> {


String _output = "0";
String _temp = "";
String _operator = "";
double _num1 = 0;
double _num2 = 0;
String _activeOperator = "";

void _buttonPressed(String value) {


setState(() {
if (value == "C") {
_output = "0";
_temp = "";
_operator = "";
_activeOperator = "";
_num1 = 0;
_num2 = 0;
} else if (value == "⌫") {
if (_temp.isNotEmpty) {
_temp = _temp.substring(0, _temp.length - 1); // Remove last
character from _temp
}
_output = _temp.isEmpty ? "0" : _temp; // Update _output based on
_temp
} else if (value == "+" || value == "-" || value == "x" || value == "/")
{
_num1 = double.parse(_output);
_operator = value;
_activeOperator = value;
_temp = "";
} else if (value == "=") {
_num2 = double.parse(_temp);
switch (_operator) {
case "+":
_output = (_num1 + _num2).toString();
break;
case "-":
_output = (_num1 - _num2).toString();
break;
case "x":
_output = (_num1 * _num2).toString();
break;
case "/":
_output = (_num1 / _num2).toString();
break;
}
if (_output.endsWith(".0")) {
_output = _output.substring(0, _output.length - 2);
}
_temp = "";
_activeOperator = "";
} else {
_temp += value;
_output = _temp;
}
});
}

Widget _buildButton(String text, Color bgColor, Color textColor, {double


widthFactor = 1, bool isOperator = false}) {
final isActive = isOperator && text == _activeOperator;
return Expanded(
flex: (widthFactor * 2).toInt(),
child: Container(
margin: EdgeInsets.all(8),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: widthFactor > 1 ? RoundedRectangleBorder(borderRadius:
BorderRadius.circular(32)) : CircleBorder(),
padding: EdgeInsets.symmetric(vertical: 24, horizontal:
widthFactor > 1 ? 80 : 24),
backgroundColor: isActive ? Colors.white : bgColor,
),
onPressed: () => _buttonPressed(text),
child: Text(
text,
style: TextStyle(
fontSize: 24,
color: isActive ? Colors.orange : textColor,
fontWeight: FontWeight.bold,
),
),
),
),
);
}

Widget _buildRow(List<String> texts, List<Color> bgColors, List<Color>


textColors, [List<double>? widths]) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: List.generate(
texts.length,
(index) => _buildButton(
texts[index],
bgColors[index],
textColors[index],
widthFactor: widths != null ? widths[index] : 1,
isOperator: ["+", "-", "x", "/"].contains(texts[index]),
),
),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
alignment: Alignment.bottomRight,
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Text(
_output,
style: TextStyle(fontSize: 60, color: Colors.white, fontWeight:
FontWeight.w200),
),
),
Divider(color: Colors.grey),
_buildRow([
"C",
"⌫",
"%",
"/",
], [
Colors.grey,
Colors.grey,
Colors.grey,
Colors.orange,
], [
Colors.black,
Colors.black,
Colors.black,
Colors.white,
]),
_buildRow([
"7",
"8",
"9",
"x",
], [
Colors.grey[850]!,
Colors.grey[850]!,
Colors.grey[850]!,
Colors.orange,
], [
Colors.white,
Colors.white,
Colors.white,
Colors.white,
]),
_buildRow([
"4",
"5",
"6",
"-",
], [
Colors.grey[850]!,
Colors.grey[850]!,
Colors.grey[850]!,
Colors.orange,
], [
Colors.white,
Colors.white,
Colors.white,
Colors.white,
]),
_buildRow([
"1",
"2",
"3",
"+",
], [
Colors.grey[850]!,
Colors.grey[850]!,
Colors.grey[850]!,
Colors.orange,
], [
Colors.white,
Colors.white,
Colors.white,
Colors.white,
]),
_buildRow([
"0",
".",
"=",
], [
Colors.grey[850]!,
Colors.grey[850]!,
Colors.orange,
], [
Colors.white,
Colors.white,
Colors.white,
], [
2, // Width factor for 0 button
1,
1,
]),
],
),
);
}
}

Natija:

You might also like