Created
January 22, 2025 01:42
-
-
Save Lxxyx/e087578f053330903f977a9469112c7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Calculator', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const CalculatorHomePage(), | |
); | |
} | |
} | |
class CalculatorHomePage extends StatefulWidget { | |
const CalculatorHomePage({super.key}); | |
@override | |
_CalculatorHomePageState createState() => _CalculatorHomePageState(); | |
} | |
class _CalculatorHomePageState extends State<CalculatorHomePage> { | |
final TextEditingController _controller = TextEditingController(); | |
double _num1 = 0, _num2 = 0, _result = 0; | |
String _operator = ''; | |
void _updateControllerText(String text) { | |
_controller.text = text; | |
} | |
void _calculateResult() { | |
if (_operator == '+') { | |
_result = _num1 + _num2; | |
} else if (_operator == '-') { | |
_result = _num1 - _num2; | |
} else if (_operator == '*') { | |
_result = _num1 * _num2; | |
} else if (_operator == '/') { | |
if (_num2 != 0) { | |
_result = _num1 / _num2; | |
} else { | |
_result = 0; | |
} | |
} | |
_updateControllerText(_result.toString()); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Calculator'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(20.0), | |
child: Column( | |
children: [ | |
TextField( | |
controller: _controller, | |
readOnly: true, | |
decoration: const InputDecoration( | |
border: OutlineInputBorder(), | |
), | |
), | |
const SizedBox(height: 20), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
_buildNumberButton('7'), | |
_buildNumberButton('8'), | |
_buildNumberButton('9'), | |
_buildOperatorButton('/'), | |
], | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
_buildNumberButton('4'), | |
_buildNumberButton('5'), | |
_buildNumberButton('6'), | |
_buildOperatorButton('*'), | |
], | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
_buildNumberButton('1'), | |
_buildNumberButton('2'), | |
_buildNumberButton('3'), | |
_buildOperatorButton('-'), | |
], | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
_buildNumberButton('0'), | |
_buildNumberButton('.'), | |
_buildEqualButton(), | |
_buildOperatorButton('+'), | |
], | |
), | |
], | |
), | |
), | |
); | |
} | |
Widget _buildNumberButton(String text) { | |
return Expanded( | |
child: ElevatedButton( | |
onPressed: () { | |
if (_operator.isEmpty) { | |
_num1 = _num1 * 10 + int.parse(text); | |
} else { | |
_num2 = _num2 * 10 + int.parse(text); | |
} | |
_updateControllerText(_num1.toString() + _operator + _num2.toString()); | |
}, | |
child: Text(text), | |
), | |
); | |
} | |
Widget _buildOperatorButton(String text) { | |
return Expanded( | |
child: ElevatedButton( | |
onPressed: () { | |
_operator = text; | |
_updateControllerText(_num1.toString() + _operator); | |
}, | |
child: Text(text), | |
), | |
); | |
} | |
Widget _buildEqualButton() { | |
return Expanded( | |
child: ElevatedButton( | |
onPressed: _calculateResult, | |
child: const Text('='), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment