This commit is contained in:
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