Flutter project
Team members
Dhaneesha tk
Arshana S
import 'package:flutter/material.dart';
import 'package:math_expressions/math_expressions.dart';
void main() {
runApp(const CalculatorApp());
}
class CalculatorApp extends StatelessWidget {
const CalculatorApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Calculator',
theme: ThemeData.dark(),
home: const CalculatorScreen(),
);
}
}
class CalculatorScreen extends StatefulWidget {
const CalculatorScreen({super.key});
@override
State<CalculatorScreen> createState() =>
_CalculatorScreenState();
}
class _CalculatorScreenState extends State<CalculatorScreen> {
String _input = ''; // Holds the current input
String _output = ''; // Holds the result
List<String> _history = []; // Stores calculation history
// Method to handle button press
void _buttonPressed(String text) {
setState(() {
_input += text;
});
}
Flutter project
Team members
Dhaneesha tk
Arshana S
// Method to handle clear button press
void _clear() {
setState(() {
_input = '';
_output = '';
});
}
// Method to handle calculation
void _calculate() {
try {
Parser p = Parser();
Expression exp = p.parse(_input);
double eval = exp.evaluate(EvaluationType.REAL,
ContextModel());
setState(() {
_output = eval.toString();
_history.insert(0, '$_input = $_output');
_input = '';
});
} catch (e) {
setState(() {
_output = 'Error';
});
}
}
// Method to show history dialog
void _showHistory() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Calculation History'),
content: SizedBox(
height: 300,
width: 200,
child: ListView.builder(
Flutter project
Team members
Dhaneesha tk
Arshana S
itemCount: _history.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_history[index]),
);
},
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
);
},
);
}
// Create buttons for the calculator
Widget _buildButton(String text, {Color color =
Colors.blue}) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
backgroundColor: color, // Use backgroundColor instead
of primary
),
onPressed: () {
if (text == "=") {
_calculate();
} else if (text == "C") {
_clear();
} else if (text == "History") {
_showHistory();
} else {
Flutter project
Team members
Dhaneesha tk
Arshana S
_buttonPressed(text);
}
},
child: Text(
text,
style: const TextStyle(fontSize: 24),
),
);
}
// Layout for calculator buttons
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Calculator'),
actions: [
IconButton(
icon: const Icon(Icons.history),
onPressed: _showHistory,
),
],
),
body: Column(
children: [
// Display area for input and output
Container(
padding: const EdgeInsets.all(20),
alignment: Alignment.centerRight,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
_input,
style: const TextStyle(fontSize: 32),
),
Text(
_output,
Flutter project
Team members
Dhaneesha tk
Arshana S
style: const TextStyle(fontSize: 28, color:
Colors.green),
),
],
),
),
// Buttons grid
Expanded(
child: GridView.builder(
padding: const EdgeInsets.all(20),
gridDelegate: const
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
itemCount: 20,
itemBuilder: (context, index) {
const buttons = [
'7',
'8',
'9',
'/',
'4',
'5',
'6',
'*',
'1',
'2',
'3',
'-',
'C',
'0',
'=',
'+',
'sin',
'cos',
'tan',
Flutter project
Team members
Dhaneesha tk
Arshana S
'History'
];
return _buildButton(buttons[index]);
},
),
),
],
),
);
}
}