{"id":3908,"date":"2023-11-04T23:13:55","date_gmt":"2023-11-04T23:13:55","guid":{"rendered":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/"},"modified":"2023-11-05T05:48:28","modified_gmt":"2023-11-05T05:48:28","slug":"how-to-make-a-calculator-app-with-flutter","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/","title":{"rendered":"How to Make a Calculator App with Flutter"},"content":{"rendered":"

In this tutorial, we will guide you on how to create a calculator app using Flutter. Flutter is an open-source UI framework developed by Google for crafting beautiful, natively compiled applications for mobile, web, and desktop platforms from a single codebase.<\/p>\n

The calculator app we will build will be able to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. We will also implement a clear button to reset the calculator display.<\/p>\n

Before we get started, ensure that you have Flutter installed on your machine. You can follow the official Flutter installation guide for instructions on how to set up Flutter: Flutter Installation Guide<\/a><\/p>\n

Step 1: Create a new Flutter project<\/h2>\n

To start, open your terminal or command prompt and navigate to the directory where you want to create your new Flutter project. Use the following command to create a new Flutter project:<\/p>\n

flutter create calculator_app\n<\/code><\/pre>\n

This command will create a new directory named calculator_app<\/code> with the basic project structure and files.<\/p>\n

Step 2: Set up the project dependencies<\/h2>\n

After creating the Flutter project, open it in your preferred code editor. Then, open the pubspec.yaml<\/code> file located in the root of your project.<\/p>\n

Add the following line under the dependencies<\/code> section:<\/p>\n

math_expressions: ^2.0.3\n<\/code><\/pre>\n

This package will help us evaluate the mathematical expressions entered by the user.<\/p>\n

Save the pubspec.yaml<\/code> file, and run the following command in the terminal to fetch the newly added dependency:<\/p>\n

flutter pub get\n<\/code><\/pre>\n

Step 3: Design the user interface<\/h2>\n

We will now design the user interface (UI) of our calculator app. Navigate to the lib<\/code> directory of your project and open the main.dart<\/code> file.<\/p>\n

Remove the default code in the main.dart<\/code> file, and replace it with the following code:<\/p>\n

import 'package:flutter\/material.dart';\nimport 'package:math_expressions\/math_expressions.dart';\n\nvoid main() {\n  runApp(CalculatorApp());\n}\n\nclass CalculatorApp extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: 'Calculator',\n      theme: ThemeData(\n        primarySwatch: Colors.blue,\n      ),\n      home: CalculatorHomePage(),\n    );\n  }\n}\n\nclass CalculatorHomePage extends StatefulWidget {\n  @override\n  _CalculatorHomePageState createState() => _CalculatorHomePageState();\n}\n\nclass _CalculatorHomePageState extends State<CalculatorHomePage> {\n  String userInput = '';\n  String result = '';\n\n  void evaluateResult() {\n    Parser p = Parser();\n    Expression exp = p.parse(userInput);\n    ContextModel cm = ContextModel();\n    double eval = exp.evaluate(EvaluationType.REAL, cm);\n    setState(() {\n      result = eval.toString();\n    });\n  }\n\n  void clear() {\n    setState(() {\n      userInput = '';\n      result = '';\n    });\n  }\n\n  Widget buildButton(String buttonText, Color buttonColor,\n      {bool isClearButton = false}) {\n    return Expanded(\n      child: Container(\n        margin: EdgeInsets.all(8.0),\n        child: FlatButton(\n          child: Text(\n            buttonText,\n            style: TextStyle(fontSize: 20.0, color: Colors.white),\n          ),\n          onPressed: () {\n            setState(() {\n              if (isClearButton) {\n                clear();\n              } else {\n                userInput += buttonText;\n                evaluateResult();\n              }\n            });\n          },\n        ),\n        color: buttonColor,\n      ),\n    );\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar: AppBar(\n        title: Text('Calculator'),\n      ),\n      body: Column(\n        children: <Widget>[\n          Expanded(\n            child: Container(\n              padding: EdgeInsets.all(16.0),\n              alignment: Alignment.bottomRight,\n              child: Text(\n                userInput,\n                style: TextStyle(fontSize: 32.0),\n              ),\n            ),\n          ),\n          Expanded(\n            child: Container(\n              padding: EdgeInsets.all(16.0),\n              alignment: Alignment.bottomRight,\n              child: Text(\n                result,\n                style: TextStyle(\n                  fontSize: 32.0,\n                  fontWeight: FontWeight.bold,\n                ),\n              ),\n            ),\n          ),\n          Row(\n            children: <Widget>[\n              buildButton('7', Colors.grey.shade700),\n              buildButton('8', Colors.grey.shade700),\n              buildButton('9', Colors.grey.shade700),\n              buildButton('\/', Colors.orange.shade700),\n            ],\n          ),\n          Row(\n            children: <Widget>[\n              buildButton('4', Colors.grey.shade700),\n              buildButton('5', Colors.grey.shade700),\n              buildButton('6', Colors.grey.shade700),\n              buildButton('*', Colors.orange.shade700),\n            ],\n          ),\n          Row(\n            children: <Widget>[\n              buildButton('1', Colors.grey.shade700),\n              buildButton('2', Colors.grey.shade700),\n              buildButton('3', Colors.grey.shade700),\n              buildButton('-', Colors.orange.shade700),\n            ],\n          ),\n          Row(\n            children: <Widget>[\n              buildButton('0', Colors.grey.shade700),\n              buildButton('.', Colors.grey.shade700),\n              buildButton('=', Colors.red.shade600),\n              buildButton('+', Colors.orange.shade700),\n            ],\n          ),\n          Row(\n            children: <Widget>[\n              buildButton('C', Colors.red.shade600, isClearButton: true),\n            ],\n          ),\n        ],\n      ),\n    );\n  }\n}\n<\/code><\/pre>\n

In this code, we have defined the CalculatorApp<\/code> class, which is the entry point of our Flutter app. Inside this class, we have defined a stateful widget named CalculatorHomePage<\/code>.<\/p>\n

The CalculatorHomePage<\/code> widget contains the UI layout for our calculator app. It has two Text<\/code> widgets to display the user input and the result. The user input is stored in the userInput<\/code> variable, and the result is stored in the result<\/code> variable.<\/p>\n

We have also defined various helper methods to handle button presses and evaluate the mathematical expression using the math_expressions<\/code> package.<\/p>\n

The buildButton<\/code> method is responsible for creating the calculator buttons. Each button is represented by a FlatButton<\/code> wrapped inside a Container<\/code> with a specified color.<\/p>\n

Step 4: Run the app<\/h2>\n

Congratulations! You have completed the coding part. Now, it’s time to run our calculator app.<\/p>\n

In the terminal, navigate to the root directory of your Flutter project and run the following command:<\/p>\n

flutter run\n<\/code><\/pre>\n

This command will compile the code and launch the app on an available simulator or connected device. You will see your calculator app running on the screen.<\/p>\n

Conclusion<\/h2>\n

In this tutorial, we learned how to create a calculator app using Flutter. We used Flutter’s UI framework to design the user interface and implemented the basic arithmetic operations.<\/p>\n

Flutter enables developers to build high-quality apps for multiple platforms using a single codebase. By enhancing our calculator app, you can add more advanced features like parentheses handling, memory functions, and scientific calculations.<\/p>\n

You can find the complete source code of the calculator app on GitHub: Calculator App Source Code<\/a><\/p>\n

I hope you found this tutorial helpful in getting started with Flutter and building your own calculator app. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"

In this tutorial, we will guide you on how to create a calculator app using Flutter. Flutter is an open-source UI framework developed by Google for crafting beautiful, natively compiled applications for mobile, web, and desktop platforms from a single codebase. The calculator app we will build will be able Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[271,266,269,268,270,272,267,265],"yoast_head":"\nHow to Make a Calculator App with Flutter - Pantherax Blogs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Make a Calculator App with Flutter\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we will guide you on how to create a calculator app using Flutter. Flutter is an open-source UI framework developed by Google for crafting beautiful, natively compiled applications for mobile, web, and desktop platforms from a single codebase. The calculator app we will build will be able Continue Reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/\" \/>\n<meta property=\"og:site_name\" content=\"Pantherax Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T23:13:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-05T05:48:28+00:00\" \/>\n<meta name=\"author\" content=\"Panther\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Panther\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t \"@context\": \"https:\/\/schema.org\",\n\t \"@graph\": [\n\t {\n\t \"@type\": \"Article\",\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/#article\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/\"\n\t },\n\t \"author\": {\n\t \"name\": \"Panther\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\"\n\t },\n\t \"headline\": \"How to Make a Calculator App with Flutter\",\n\t \"datePublished\": \"2023-11-04T23:13:55+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:28+00:00\",\n\t \"mainEntityOfPage\": {\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/\"\n\t },\n\t \"wordCount\": 546,\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"keywords\": [\n\t \"\\\"Beginner's guide for Flutter calculator app\\\"\",\n\t \"\\\"Build calculator app with Flutter\\\"\",\n\t \"\\\"Calculator app development with Flutter\\\"\",\n\t \"\\\"Create calculator app using Flutter\\\"\",\n\t \"\\\"Flutter app development tutorial\\\"\",\n\t \"\\\"Flutter calculator app example\\\"]\",\n\t \"\\\"Step by step tutorial for calculator app\\\"\",\n\t \"[\\\"Flutter calculator app\\\"\"\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"WebPage\",\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/\",\n\t \"url\": \"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/\",\n\t \"name\": \"How to Make a Calculator App with Flutter - Pantherax Blogs\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#website\"\n\t },\n\t \"datePublished\": \"2023-11-04T23:13:55+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:28+00:00\",\n\t \"breadcrumb\": {\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/#breadcrumb\"\n\t },\n\t \"inLanguage\": \"en-US\",\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"ReadAction\",\n\t \"target\": [\n\t \"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/\"\n\t ]\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"BreadcrumbList\",\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/#breadcrumb\",\n\t \"itemListElement\": [\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 1,\n\t \"name\": \"Home\",\n\t \"item\": \"http:\/\/localhost:10003\/\"\n\t },\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 2,\n\t \"name\": \"How to Make a Calculator App with Flutter\"\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"WebSite\",\n\t \"@id\": \"http:\/\/localhost:10003\/#website\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"description\": \"\",\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"SearchAction\",\n\t \"target\": {\n\t \"@type\": \"EntryPoint\",\n\t \"urlTemplate\": \"http:\/\/localhost:10003\/?s={search_term_string}\"\n\t },\n\t \"query-input\": \"required name=search_term_string\"\n\t }\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"Organization\",\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"logo\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\",\n\t \"url\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"contentUrl\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"width\": 1024,\n\t \"height\": 1024,\n\t \"caption\": \"Pantherax Blogs\"\n\t },\n\t \"image\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\"\n\t }\n\t },\n\t {\n\t \"@type\": \"Person\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\",\n\t \"name\": \"Panther\",\n\t \"image\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/image\/\",\n\t \"url\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"contentUrl\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"caption\": \"Panther\"\n\t },\n\t \"sameAs\": [\n\t \"http:\/\/localhost:10003\"\n\t ],\n\t \"url\": \"http:\/\/localhost:10003\/author\/pepethefrog\/\"\n\t }\n\t ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Make a Calculator App with Flutter - Pantherax Blogs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/","og_locale":"en_US","og_type":"article","og_title":"How to Make a Calculator App with Flutter","og_description":"In this tutorial, we will guide you on how to create a calculator app using Flutter. Flutter is an open-source UI framework developed by Google for crafting beautiful, natively compiled applications for mobile, web, and desktop platforms from a single codebase. The calculator app we will build will be able Continue Reading","og_url":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/","og_site_name":"Pantherax Blogs","article_published_time":"2023-11-04T23:13:55+00:00","article_modified_time":"2023-11-05T05:48:28+00:00","author":"Panther","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Panther","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/#article","isPartOf":{"@id":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/"},"author":{"name":"Panther","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7"},"headline":"How to Make a Calculator App with Flutter","datePublished":"2023-11-04T23:13:55+00:00","dateModified":"2023-11-05T05:48:28+00:00","mainEntityOfPage":{"@id":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/"},"wordCount":546,"publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"keywords":["\"Beginner's guide for Flutter calculator app\"","\"Build calculator app with Flutter\"","\"Calculator app development with Flutter\"","\"Create calculator app using Flutter\"","\"Flutter app development tutorial\"","\"Flutter calculator app example\"]","\"Step by step tutorial for calculator app\"","[\"Flutter calculator app\""],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/","url":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/","name":"How to Make a Calculator App with Flutter - Pantherax Blogs","isPartOf":{"@id":"http:\/\/localhost:10003\/#website"},"datePublished":"2023-11-04T23:13:55+00:00","dateModified":"2023-11-05T05:48:28+00:00","breadcrumb":{"@id":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/localhost:10003\/how-to-make-a-calculator-app-with-flutter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/localhost:10003\/"},{"@type":"ListItem","position":2,"name":"How to Make a Calculator App with Flutter"}]},{"@type":"WebSite","@id":"http:\/\/localhost:10003\/#website","url":"http:\/\/localhost:10003\/","name":"Pantherax Blogs","description":"","publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/localhost:10003\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/localhost:10003\/#organization","name":"Pantherax Blogs","url":"http:\/\/localhost:10003\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/","url":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","contentUrl":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","width":1024,"height":1024,"caption":"Pantherax Blogs"},"image":{"@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7","name":"Panther","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","caption":"Panther"},"sameAs":["http:\/\/localhost:10003"],"url":"http:\/\/localhost:10003\/author\/pepethefrog\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3908"}],"collection":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/comments?post=3908"}],"version-history":[{"count":1,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3908\/revisions"}],"predecessor-version":[{"id":4644,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3908\/revisions\/4644"}],"wp:attachment":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/media?parent=3908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/categories?post=3908"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/tags?post=3908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}