Last active
February 7, 2022 12:04
-
-
Save hman278/8cf8849172899e8c5d5c327186e2bd47 to your computer and use it in GitHub Desktop.
calculator on flutter
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'; | |
import 'package:flutter_layout_grid/flutter_layout_grid.dart'; | |
import 'package:math_expressions/math_expressions.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
void refresh() { | |
setState(() {}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold( | |
appBar: AppBar(title: const Text('Calculator')), | |
body: | |
Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [ | |
Expanded( | |
child: Text(CalcOperations.operationStr, | |
style: const TextStyle( | |
fontSize: 150, | |
fontWeight: FontWeight.w100, | |
color: Colors.blueAccent)), | |
), | |
Expanded( | |
flex: 2, | |
child: LayoutGrid(areas: ''' | |
buttonSeven buttonEight buttonNine buttonMinus | |
buttonFour buttonFive buttonSix buttonPlus | |
buttonOne buttonTwo buttonThree buttonDivide | |
buttonClear buttonZero buttonEqual buttonMultiply | |
''', rowSizes: const [ | |
FlexibleTrackSize(100), | |
FlexibleTrackSize(100), | |
FlexibleTrackSize(100), | |
FlexibleTrackSize(100), | |
], columnSizes: const [ | |
FlexibleTrackSize(100), | |
FlexibleTrackSize(100), | |
FlexibleTrackSize(100), | |
FlexibleTrackSize(100), | |
], children: [ | |
CalcButton(value: 7, notifyParent: refresh) | |
.inGridArea('buttonSeven'), | |
CalcButton(value: 8, notifyParent: refresh) | |
.inGridArea('buttonEight'), | |
CalcButton(value: 9, notifyParent: refresh) | |
.inGridArea('buttonNine'), | |
CalcButton(value: '-', notifyParent: refresh) | |
.inGridArea('buttonMinus'), | |
CalcButton(value: 4, notifyParent: refresh) | |
.inGridArea('buttonFour'), | |
CalcButton(value: 5, notifyParent: refresh) | |
.inGridArea('buttonFive'), | |
CalcButton(value: 6, notifyParent: refresh) | |
.inGridArea('buttonSix'), | |
CalcButton(value: '+', notifyParent: refresh) | |
.inGridArea('buttonPlus'), | |
CalcButton(value: 1, notifyParent: refresh) | |
.inGridArea('buttonOne'), | |
CalcButton(value: 2, notifyParent: refresh) | |
.inGridArea('buttonTwo'), | |
CalcButton(value: 3, notifyParent: refresh) | |
.inGridArea('buttonThree'), | |
CalcButton(value: '/', notifyParent: refresh) | |
.inGridArea('buttonDivide'), | |
CalcButton(value: 'C', notifyParent: refresh) | |
.inGridArea('buttonClear'), | |
CalcButton(value: 0, notifyParent: refresh) | |
.inGridArea('buttonZero'), | |
CalcButton(value: '=', notifyParent: refresh) | |
.inGridArea('buttonEqual'), | |
CalcButton(value: '*', notifyParent: refresh) | |
.inGridArea('buttonMultiply'), | |
]), | |
), | |
]), | |
)); | |
} | |
} | |
class CalcButton extends StatelessWidget { | |
const CalcButton({Key? key, required this.value, required this.notifyParent}) | |
: super(key: key); | |
final Object value; | |
final Function() notifyParent; | |
@override | |
Widget build(BuildContext context) { | |
return OutlinedButton( | |
style: ButtonStyle(minimumSize: MaterialStateProperty.all(Size.infinite)), | |
child: Text(value.toString()), | |
onPressed: () { | |
if (value == '=' && CalcOperations.operationStr != '') { | |
CalcOperations.operationStr = Parser() | |
.parse(CalcOperations.operationStr) | |
.evaluate(EvaluationType.REAL, ContextModel()) | |
.round() | |
.toString(); | |
notifyParent(); | |
return; | |
} | |
CalcOperations.operationStr += value.toString(); | |
notifyParent(); | |
}, | |
); | |
} | |
} | |
class CalcOperations { | |
static String operationStr = ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment