This commit is contained in:
96
lib/calculator_logic.dart
Normal file
96
lib/calculator_logic.dart
Normal file
@@ -0,0 +1,96 @@
|
||||
class CalculatorLogic {
|
||||
String _display = '0';
|
||||
double _firstOperand = 0;
|
||||
String _operator = '';
|
||||
bool _waitingForSecondOperand = false;
|
||||
|
||||
String get display => _display;
|
||||
|
||||
void inputDigit(String digit) {
|
||||
if (_waitingForSecondOperand) {
|
||||
_display = digit;
|
||||
_waitingForSecondOperand = false;
|
||||
} else {
|
||||
_display = _display == '0' ? digit : _display + digit;
|
||||
}
|
||||
}
|
||||
|
||||
void inputDecimal() {
|
||||
if (_waitingForSecondOperand) {
|
||||
_display = '0.';
|
||||
_waitingForSecondOperand = false;
|
||||
return;
|
||||
}
|
||||
if (!_display.contains('.')) {
|
||||
_display += '.';
|
||||
}
|
||||
}
|
||||
|
||||
void inputOperator(String op) {
|
||||
final current = double.parse(_display);
|
||||
|
||||
if (_operator.isNotEmpty && !_waitingForSecondOperand) {
|
||||
_firstOperand = _calculate(_firstOperand, current, _operator);
|
||||
_display = _formatResult(_firstOperand);
|
||||
} else {
|
||||
_firstOperand = current;
|
||||
}
|
||||
|
||||
_operator = op;
|
||||
_waitingForSecondOperand = true;
|
||||
}
|
||||
|
||||
void calculate() {
|
||||
if (_operator.isEmpty) return;
|
||||
|
||||
final secondOperand = double.parse(_display);
|
||||
final result = _calculate(_firstOperand, secondOperand, _operator);
|
||||
_display = _formatResult(result);
|
||||
_firstOperand = result;
|
||||
_operator = '';
|
||||
_waitingForSecondOperand = true;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_display = '0';
|
||||
_firstOperand = 0;
|
||||
_operator = '';
|
||||
_waitingForSecondOperand = false;
|
||||
}
|
||||
|
||||
void toggleSign() {
|
||||
final value = double.parse(_display);
|
||||
_display = _formatResult(-value);
|
||||
}
|
||||
|
||||
void percent() {
|
||||
final value = double.parse(_display);
|
||||
_display = _formatResult(value / 100);
|
||||
}
|
||||
|
||||
double _calculate(double a, double b, String op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return a + b;
|
||||
case '-':
|
||||
return a - b;
|
||||
case '*':
|
||||
return a * b;
|
||||
case '/':
|
||||
if (b == 0) return double.infinity;
|
||||
return a / b;
|
||||
default:
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
String _formatResult(double value) {
|
||||
if (value.isInfinite || value.isNaN) {
|
||||
return value.toString();
|
||||
}
|
||||
if (value == value.toInt().toDouble()) {
|
||||
return value.toInt().toString();
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
159
lib/main.dart
Normal file
159
lib/main.dart
Normal file
@@ -0,0 +1,159 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'calculator_logic.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const KalkulatorApp());
|
||||
}
|
||||
|
||||
class KalkulatorApp extends StatelessWidget {
|
||||
const KalkulatorApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Kalkulator',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: Colors.blueGrey,
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: const CalculatorScreen(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalculatorScreen extends StatefulWidget {
|
||||
const CalculatorScreen({super.key});
|
||||
|
||||
@override
|
||||
State<CalculatorScreen> createState() => _CalculatorScreenState();
|
||||
}
|
||||
|
||||
class _CalculatorScreenState extends State<CalculatorScreen> {
|
||||
final _logic = CalculatorLogic();
|
||||
|
||||
void _onPressed(String label) {
|
||||
setState(() {
|
||||
switch (label) {
|
||||
case 'C':
|
||||
_logic.clear();
|
||||
case '+/-':
|
||||
_logic.toggleSign();
|
||||
case '%':
|
||||
_logic.percent();
|
||||
case '=':
|
||||
_logic.calculate();
|
||||
case '.':
|
||||
_logic.inputDecimal();
|
||||
case '+' || '-' || '*' || '/':
|
||||
_logic.inputOperator(label);
|
||||
default:
|
||||
_logic.inputDigit(label);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Container(
|
||||
alignment: Alignment.bottomRight,
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text(
|
||||
_logic.display,
|
||||
style: const TextStyle(
|
||||
fontSize: 64,
|
||||
fontWeight: FontWeight.w300,
|
||||
color: Colors.white,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildButtonRow(['C', '+/-', '%', '/']),
|
||||
_buildButtonRow(['7', '8', '9', '*']),
|
||||
_buildButtonRow(['4', '5', '6', '-']),
|
||||
_buildButtonRow(['1', '2', '3', '+']),
|
||||
_buildButtonRow(['0', '.', '=']),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildButtonRow(List<String> labels) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(
|
||||
children: labels.map((label) {
|
||||
final isWide = label == '0' && labels.length == 3;
|
||||
return Expanded(
|
||||
flex: isWide ? 2 : 1,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(4),
|
||||
child: _CalcButton(
|
||||
label: label,
|
||||
onPressed: () => _onPressed(label),
|
||||
color: _buttonColor(label),
|
||||
textColor: _textColor(label),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Color _buttonColor(String label) {
|
||||
if (['+', '-', '*', '/', '='].contains(label)) return Colors.orange;
|
||||
if (['C', '+/-', '%'].contains(label)) return Colors.grey.shade700;
|
||||
return Colors.grey.shade900;
|
||||
}
|
||||
|
||||
Color _textColor(String label) {
|
||||
if (['C', '+/-', '%'].contains(label)) return Colors.black;
|
||||
return Colors.white;
|
||||
}
|
||||
}
|
||||
|
||||
class _CalcButton extends StatelessWidget {
|
||||
final String label;
|
||||
final VoidCallback onPressed;
|
||||
final Color color;
|
||||
final Color textColor;
|
||||
|
||||
const _CalcButton({
|
||||
required this.label,
|
||||
required this.onPressed,
|
||||
required this.color,
|
||||
required this.textColor,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ElevatedButton(
|
||||
onPressed: onPressed,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: color,
|
||||
foregroundColor: textColor,
|
||||
padding: const EdgeInsets.all(20),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 28, fontWeight: FontWeight.w400),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user