├── calc.PNG ├── main.dart ├── pubspec.yaml └── readme.md /calc.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chandran-jr/Calculator/7451d7dab47f522c64a3949441c55e98b088f0cb/calc.PNG -------------------------------------------------------------------------------- /main.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/cupertino.dart'; 2 | import 'package:flutter/material.dart'; 3 | import 'package:math_expressions/math_expressions.dart'; 4 | 5 | void main(){ 6 | runApp(Calculator()); 7 | 8 | } 9 | 10 | class Calculator extends StatelessWidget{ 11 | @override 12 | Widget build(BuildContext context){ 13 | return MaterialApp( 14 | debugShowCheckedModeBanner: false, 15 | title: 'Calculator', 16 | theme:ThemeData(primarySwatch: Colors.green), 17 | home: SimpleCalculator(), 18 | ); 19 | } 20 | } 21 | 22 | class SimpleCalculator extends StatefulWidget{ 23 | @override 24 | _SimpleCalculatorState createState() => _SimpleCalculatorState(); 25 | } 26 | 27 | class _SimpleCalculatorState extends State { 28 | 29 | String equation = "0"; 30 | String result = "0"; 31 | String expression = ""; 32 | double equationFontSize = 38.0; 33 | double resultFontSize = 48.0; 34 | 35 | buttonPressed(String buttonText){ 36 | setState(() { 37 | if(buttonText=="C"){ 38 | equation="0"; 39 | result="0"; 40 | equationFontSize = 38.0; 41 | resultFontSize = 48.0; 42 | } 43 | else if(buttonText=="DEL"){ 44 | equationFontSize = 48.0; 45 | resultFontSize = 38.0; 46 | equation = equation.substring(0, equation.length - 1); 47 | if(equation==""){ 48 | equation="0"; 49 | } 50 | } 51 | else if(buttonText=="="){ 52 | equationFontSize = 38.0; 53 | resultFontSize = 48.0; 54 | expression = equation; 55 | 56 | try{ 57 | Parser p = Parser(); 58 | Expression exp = p.parse(expression); 59 | 60 | ContextModel cm = ContextModel(); 61 | result = '${exp.evaluate(EvaluationType.REAL, cm)}'; 62 | 63 | }catch(e){ 64 | result="Error"; 65 | } 66 | 67 | 68 | } 69 | else { 70 | equationFontSize = 48.0; 71 | resultFontSize = 38.0; 72 | if (equation == "0") { 73 | equation = buttonText; 74 | } 75 | else { 76 | equation = equation + buttonText; 77 | } 78 | }}); 79 | 80 | } 81 | 82 | 83 | 84 | Widget buildButton(String buttonText,double buttonHeight, Color buttonColor){ 85 | return Container( 86 | height: MediaQuery.of(context).size.height*0.1*buttonHeight, 87 | color: buttonColor, 88 | child: FlatButton( 89 | shape: RoundedRectangleBorder( 90 | borderRadius: BorderRadius.circular(0.0), 91 | side: BorderSide( 92 | color: Colors.white, 93 | width: 1, 94 | style: BorderStyle.solid 95 | ) 96 | ), 97 | padding: EdgeInsets.all(16.0), 98 | onPressed: () => buttonPressed(buttonText), 99 | child: Text( 100 | buttonText, 101 | style: TextStyle( 102 | fontSize: 30.0, 103 | fontWeight: FontWeight.normal, 104 | color: Colors.white 105 | ), 106 | ) 107 | ), 108 | ); 109 | } 110 | @override 111 | Widget build(BuildContext context) { 112 | // TODO: implement build 113 | return Scaffold( 114 | appBar: AppBar(title: Text('Calculator')), 115 | body: Column( 116 | children: [ 117 | Container( 118 | alignment: Alignment.centerRight, 119 | padding: EdgeInsets.fromLTRB(10, 20, 10, 0), 120 | child: Text(equation,style: TextStyle(fontSize: equationFontSize),), 121 | ), 122 | Container( 123 | alignment: Alignment.centerRight, 124 | padding: EdgeInsets.fromLTRB(10, 20, 10, 0), 125 | child: Text(result,style: TextStyle(fontSize: resultFontSize),), 126 | ), 127 | Expanded( 128 | child: Divider(), 129 | ), 130 | Row( 131 | mainAxisAlignment: MainAxisAlignment.center, 132 | children: [ 133 | Container( 134 | width: MediaQuery.of(context).size.width*0.75, 135 | child: Table( 136 | children: [ 137 | TableRow( 138 | children: [ 139 | buildButton("C", 1, Colors.redAccent), 140 | buildButton("DEL", 1, Colors.redAccent), 141 | buildButton("/", 1, Colors.blue), 142 | ] 143 | ), 144 | 145 | TableRow( 146 | children: [ 147 | buildButton("7", 1, Colors.black54), 148 | buildButton("8", 1, Colors.black54), 149 | buildButton("9", 1, Colors.black54), 150 | ] 151 | ), 152 | 153 | TableRow( 154 | children: [ 155 | buildButton("4", 1, Colors.black54), 156 | buildButton("5", 1, Colors.black54), 157 | buildButton("6", 1, Colors.black54), 158 | ] 159 | ), 160 | 161 | TableRow( 162 | children: [ 163 | buildButton("1", 1, Colors.black54), 164 | buildButton("2", 1, Colors.black54), 165 | buildButton("3", 1, Colors.black54), 166 | ] 167 | ), 168 | 169 | TableRow( 170 | children: [ 171 | buildButton(".", 1, Colors.black54), 172 | buildButton("0", 1, Colors.black54), 173 | buildButton("00", 1, Colors.black54), 174 | ] 175 | ), 176 | ], 177 | ), 178 | ), 179 | 180 | 181 | Container( 182 | width: MediaQuery.of(context).size.width*0.25, 183 | child: Table( 184 | children: [ 185 | TableRow( 186 | children: [ 187 | buildButton("*", 1, Colors.blue), 188 | ] 189 | ), 190 | 191 | TableRow( 192 | children: [ 193 | buildButton("-", 1, Colors.blue), 194 | ] 195 | ), 196 | 197 | TableRow( 198 | children: [ 199 | buildButton("+", 1, Colors.blue), 200 | ] 201 | ), 202 | 203 | TableRow( 204 | children: [ 205 | buildButton("=", 2, Colors.green), 206 | ] 207 | ), 208 | ], 209 | ), 210 | ) 211 | ], 212 | ), 213 | ], 214 | ), 215 | 216 | ); 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: calc 2 | description: A Calculator app using flutter. 3 | 4 | # The following defines the version and build number for your application. 5 | # A version number is three numbers separated by dots, like 1.2.43 6 | # followed by an optional build number separated by a +. 7 | # Both the version and the builder number may be overridden in flutter 8 | # build by specifying --build-name and --build-number, respectively. 9 | # In Android, build-name is used as versionName while build-number used as versionCode. 10 | # Read more about Android versioning at https://developer.android.com/studio/publish/versioning 11 | # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. 12 | # Read more about iOS versioning at 13 | # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html 14 | version: 1.0.0+1 15 | 16 | environment: 17 | sdk: ">=2.1.0 <3.0.0" 18 | 19 | dependencies: 20 | flutter: 21 | sdk: flutter 22 | 23 | # The following adds the Cupertino Icons font to your application. 24 | # Use with the CupertinoIcons class for iOS style icons. 25 | cupertino_icons: ^0.1.2 26 | math_expressions: ^2.0.0 27 | 28 | dev_dependencies: 29 | flutter_test: 30 | sdk: flutter 31 | 32 | 33 | # For information on the generic Dart part of this file, see the 34 | # following page: https://dart.dev/tools/pub/pubspec 35 | 36 | # The following section is specific to Flutter. 37 | flutter: 38 | 39 | # The following line ensures that the Material Icons font is 40 | # included with your application, so that you can use the icons in 41 | # the material Icons class. 42 | uses-material-design: true 43 | 44 | # To add assets to your application, add an assets section, like this: 45 | # assets: 46 | # - images/a_dot_burr.jpeg 47 | # - images/a_dot_ham.jpeg 48 | 49 | # An image asset can refer to one or more resolution-specific "variants", see 50 | # https://flutter.dev/assets-and-images/#resolution-aware. 51 | 52 | # For details regarding adding assets from package dependencies, see 53 | # https://flutter.dev/assets-and-images/#from-packages 54 | 55 | # To add custom fonts to your application, add a fonts section here, 56 | # in this "flutter" section. Each entry in this list should have a 57 | # "family" key with the font family name, and a "fonts" key with a 58 | # list giving the asset and other descriptors for the font. For 59 | # example: 60 | # fonts: 61 | # - family: Schyler 62 | # fonts: 63 | # - asset: fonts/Schyler-Regular.ttf 64 | # - asset: fonts/Schyler-Italic.ttf 65 | # style: italic 66 | # - family: Trajan Pro 67 | # fonts: 68 | # - asset: fonts/TrajanPro.ttf 69 | # - asset: fonts/TrajanPro_Bold.ttf 70 | # weight: 700 71 | # 72 | # For details regarding fonts from package dependencies, 73 | # see https://flutter.dev/custom-fonts/#from-packages 74 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # A simple calculator app 2 | 3 | **I used:** 4 | * Cupertino icons 5 | * Math operations 6 | 7 | 8 | **Preview:** 9 | 10 | 11 | --------------------------------------------------------------------------------