145 lines
4.6 KiB
Dart
Executable File
145 lines
4.6 KiB
Dart
Executable File
// lib/widgets/transaction_calculator.dart
|
||
import 'package:flutter/material.dart';
|
||
|
||
class TransactionCalculator extends StatefulWidget {
|
||
final Function(double) onAmountChanged;
|
||
final VoidCallback onSave;
|
||
|
||
const TransactionCalculator({
|
||
super.key,
|
||
required this.onAmountChanged,
|
||
required this.onSave,
|
||
});
|
||
|
||
@override
|
||
State<TransactionCalculator> createState() => _TransactionCalculatorState();
|
||
}
|
||
|
||
class _TransactionCalculatorState extends State<TransactionCalculator> {
|
||
String _currentInput = '0';
|
||
bool _decimalAdded = false;
|
||
|
||
void _onNumberTap(String number) {
|
||
setState(() {
|
||
if (_currentInput == '0' && number != '.') {
|
||
_currentInput = number;
|
||
} else if (number == '.' && !_decimalAdded) {
|
||
_currentInput += number;
|
||
_decimalAdded = true;
|
||
} else if (number != '.') {
|
||
_currentInput += number;
|
||
}
|
||
widget.onAmountChanged(double.tryParse(_currentInput) ?? 0.0);
|
||
});
|
||
}
|
||
|
||
void _onClear() {
|
||
setState(() {
|
||
_currentInput = '0';
|
||
_decimalAdded = false;
|
||
widget.onAmountChanged(0.0);
|
||
});
|
||
}
|
||
|
||
void _onBackspace() {
|
||
setState(() {
|
||
if (_currentInput.length > 1) {
|
||
if (_currentInput.endsWith('.')) {
|
||
_decimalAdded = false;
|
||
}
|
||
_currentInput = _currentInput.substring(0, _currentInput.length - 1);
|
||
} else {
|
||
_currentInput = '0';
|
||
_decimalAdded = false;
|
||
}
|
||
widget.onAmountChanged(double.tryParse(_currentInput) ?? 0.0);
|
||
});
|
||
}
|
||
|
||
Widget _buildButton(String text, {Color? backgroundColor, Color? textColor, Function? onTap, int flex = 1}) {
|
||
return Expanded(
|
||
flex: flex,
|
||
child: Padding(
|
||
padding: const EdgeInsets.all(4.0),
|
||
child: Material(
|
||
color: backgroundColor ?? Colors.grey[200],
|
||
borderRadius: BorderRadius.circular(8.0),
|
||
child: InkWell(
|
||
onTap: () => onTap?.call(),
|
||
borderRadius: BorderRadius.circular(8.0),
|
||
child: Container(
|
||
height: 60,
|
||
alignment: Alignment.center,
|
||
child: Text(
|
||
text,
|
||
style: TextStyle(
|
||
fontSize: 24,
|
||
fontWeight: FontWeight.bold,
|
||
color: textColor ?? Colors.black,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Container(
|
||
color: Colors.white, // Fundo branco para a calculadora
|
||
padding: const EdgeInsets.all(8.0),
|
||
child: Column(
|
||
children: [
|
||
Row(
|
||
children: [
|
||
_buildButton('C', onTap: _onClear, textColor: Colors.red),
|
||
_buildButton('⌫', onTap: _onBackspace),
|
||
_buildButton('%', onTap: () { /* Implementar lógica de percentagem se necessário */ }),
|
||
_buildButton('÷', backgroundColor: Colors.blue, textColor: Colors.white, onTap: () { /* Operador */ }),
|
||
],
|
||
),
|
||
Row(
|
||
children: [
|
||
_buildButton('7', onTap: () => _onNumberTap('7')),
|
||
_buildButton('8', onTap: () => _onNumberTap('8')),
|
||
_buildButton('9', onTap: () => _onNumberTap('9')),
|
||
_buildButton('×', backgroundColor: Colors.blue, textColor: Colors.white, onTap: () { /* Operador */ }),
|
||
],
|
||
),
|
||
Row(
|
||
children: [
|
||
_buildButton('4', onTap: () => _onNumberTap('4')),
|
||
_buildButton('5', onTap: () => _onNumberTap('5')),
|
||
_buildButton('6', onTap: () => _onNumberTap('6')),
|
||
_buildButton('-', backgroundColor: Colors.blue, textColor: Colors.white, onTap: () { /* Operador */ }),
|
||
],
|
||
),
|
||
Row(
|
||
children: [
|
||
_buildButton('1', onTap: () => _onNumberTap('1')),
|
||
_buildButton('2', onTap: () => _onNumberTap('2')),
|
||
_buildButton('3', onTap: () => _onNumberTap('3')),
|
||
_buildButton('+', backgroundColor: Colors.blue, textColor: Colors.white, onTap: () { /* Operador */ }),
|
||
],
|
||
),
|
||
Row(
|
||
children: [
|
||
_buildButton('.', onTap: () => _onNumberTap('.')),
|
||
_buildButton('0', onTap: () => _onNumberTap('0')),
|
||
_buildButton('=', onTap: () { /* Implementar lógica de igual se for uma calculadora completa */ }),
|
||
_buildButton(
|
||
'OK',
|
||
backgroundColor: Colors.blue,
|
||
textColor: Colors.white,
|
||
onTap: widget.onSave,
|
||
flex: 2, // OK button takes more space
|
||
),
|
||
],
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
} |