import 'package:flutter/material.dart'; import 'package:math_expressions/math_expressions.dart'; class FikoYTFinansalKlavye extends StatefulWidget { final Function(String) onInput; final Function(double?) onCalc; final Function() onDelete; const FikoYTFinansalKlavye({ required this.onInput, required this.onCalc, required this.onDelete, super.key, }); @override State createState() => _FikoYTFinansalKlavyeState(); } class _FikoYTFinansalKlavyeState extends State { String _input = "0.0"; final List _history = []; int _historyIndex = -1; String _currency = ""; bool _soundEnabled = true; final double _fontSize = 16; void _handleInput(String value) { setState(() { if (value == '=') { _calculateResult(); } else { _input += value; } _history.add(_input); _historyIndex = _history.length - 1; }); _triggerHapticFeedback(); if (_soundEnabled) _playSound(); widget.onInput("$_input $_currency"); } void _calculateResult() { try { _input = _evaluateExpression(_input); widget.onCalc(double.tryParse(_input)); } catch (e) { _input = "Hata"; widget.onCalc(null); } } String _evaluateExpression(String expression) { try { expression = expression.replaceAll('×', '*').replaceAll('÷', '/'); return _safeEval(expression).toString(); } catch (e) { return "Hata"; } } double _safeEval(String expr) { ExpressionParser p = GrammarParser(); Expression exp = p.parse(_input); ContextModel cm = ContextModel(); double eval = exp.evaluate(EvaluationType.REAL, cm); return eval; } void _handleDelete() { setState(() { if (_input.isNotEmpty) { _input = _input.substring(0, _input.length - 1); _history.add(_input); _historyIndex = _history.length - 1; } }); _triggerHapticFeedback(); widget.onDelete(); } void _handleUndo() { if (_historyIndex > 0) { setState(() { _historyIndex--; _input = _history[_historyIndex]; }); } } void _handleRedo() { if (_historyIndex < _history.length - 1) { setState(() { _historyIndex++; _input = _history[_historyIndex]; }); } } void _toggleCurrency() { setState(() { List currencies = ['₺', '\$', '€', '£']; int currentIndex = currencies.indexOf(_currency); _currency = currencies[(currentIndex + 1) % currencies.length]; }); } void _toggleSound() { setState(() { _soundEnabled = !_soundEnabled; }); } void _triggerHapticFeedback() {} void _playSound() {} @override Widget build(BuildContext context) { return LayoutBuilder( builder: (context, constraints) { // double aspectRatio = constraints.maxHeight > 10 // ? constraints.maxWidth / (constraints.maxHeight / 5) // : 1; return Container( padding: const EdgeInsets.all(0.0), decoration: BoxDecoration( // color: Colors.grey[200], borderRadius: BorderRadius.circular(0), ), child: GridView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 4, childAspectRatio: (1 / .7), // childAspectRatio: aspectRatio, ), itemCount: 20, itemBuilder: (context, index) { List keys = [ '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', 'C', '+', '(', ')', '=', '<' ]; return GestureDetector( onTap: () { if ((_input == "Hata") || (_input == "0.0")) { _input = ""; } if (keys[index] == 'C') { setState(() => _input = "0.0"); widget.onInput("$_input $_currency"); } else if (keys[index] == '<') { _handleDelete(); widget.onInput("$_input $_currency"); } else { _handleInput(keys[index]); } if ((_input == "")) { _input = "0.0"; widget.onInput("$_input $_currency"); } }, child: Container( margin: const EdgeInsets.all(1.0), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(0), boxShadow: [ // BoxShadow( // color: Colors.black26, // blurRadius: 1, // spreadRadius: 1, // ), ], ), alignment: Alignment.center, child: Text( keys[index], style: TextStyle( fontSize: _fontSize, fontWeight: FontWeight.bold, ), ), ), ); }, ), ); }, ); } }