Advertisement
saimain-dev

Flutter Calculator Homework

Oct 26th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.97 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() => runApp(MyApp());
  4.  
  5. class MyApp extends StatefulWidget {
  6.   @override
  7.   _MyAppState createState() => _MyAppState();
  8. }
  9.  
  10. class _MyAppState extends State<MyApp> {
  11.   int firstNumber;
  12.   int secondNumber;
  13.   var result;
  14.  
  15.   TextEditingController firstTextEditingController;
  16.   TextEditingController secondTextEditingController;
  17.  
  18.   @override
  19.   void initState() {
  20.     // TODO: implement initState
  21.     super.initState();
  22.     firstTextEditingController = TextEditingController();
  23.     secondTextEditingController = TextEditingController();
  24.   }
  25.  
  26.   void changeFirstNumber(String str) {
  27.     print(str);
  28.     firstTextEditingController.text = str;
  29.   }
  30.  
  31.   void changeSecondNumber(String str) {
  32.     print(str);
  33.     secondTextEditingController.text = str;
  34.   }
  35.  
  36.   void plusClicked() {
  37.     result = int.parse(firstTextEditingController.text) +
  38.         int.parse(secondTextEditingController.text);
  39.     setState(() {});
  40.   }
  41.  
  42.   void subtractClicked() {
  43.     result = int.parse(firstTextEditingController.text) -
  44.         int.parse(secondTextEditingController.text);
  45.     setState(() {});
  46.   }
  47.  
  48.   void multiplyClicked() {
  49.     result = int.parse(firstTextEditingController.text) *
  50.         int.parse(secondTextEditingController.text);
  51.     setState(() {});
  52.   }
  53.  
  54.   void dividedClicked() {
  55.     result = int.parse(firstTextEditingController.text) /
  56.         int.parse(secondTextEditingController.text);
  57.     setState(() {});
  58.   }
  59.  
  60.   void percentClicked() {
  61.     result = int.parse(firstTextEditingController.text) %
  62.         int.parse(secondTextEditingController.text);
  63.     setState(() {});
  64.   }
  65.  
  66.   void clearText() {
  67.     firstTextEditingController.text = '';
  68.     secondTextEditingController.text = '';
  69.     result = '';
  70.     setState(() {});
  71.   }
  72.  
  73.   @override
  74.   Widget build(BuildContext context) {
  75.     return MaterialApp(
  76.       title: 'Calculator Homework',
  77.       home: Scaffold(
  78.         appBar: AppBar(
  79.           title: Text('Calculator Homework'),
  80.         ),
  81.         body: Center(
  82.           child: Container(
  83.             padding: EdgeInsets.all(30),
  84.             child: Column(
  85.               children: [
  86.                 // first input box
  87.                 TextField(
  88.                   controller: firstTextEditingController,
  89.                   keyboardType: TextInputType.number,
  90.                   onSubmitted: changeFirstNumber,
  91.                 ),
  92.                 SizedBox(
  93.                   height: 20,
  94.                 ),
  95.                 // second input box
  96.                 TextField(
  97.                   controller: secondTextEditingController,
  98.                   keyboardType: TextInputType.number,
  99.                   onSubmitted: changeSecondNumber,
  100.                 ),
  101.                 SizedBox(
  102.                   height: 20,
  103.                 ),
  104.                 //Result Text
  105.                 Text('Result : $result'),
  106.                 SizedBox(
  107.                   height: 20,
  108.                 ),
  109.                 Row(
  110.                   children: [
  111.                     Expanded(
  112.                       child: RaisedButton(
  113.                         color: Colors.blue,
  114.                         onPressed: plusClicked,
  115.                         child: Text(
  116.                           '+',
  117.                           style: TextStyle(color: Colors.white, fontSize: 20),
  118.                         ),
  119.                       ),
  120.                     ),
  121.                     SizedBox(
  122.                       width: 20,
  123.                     ),
  124.                     Expanded(
  125.                       child: RaisedButton(
  126.                         color: Colors.blue,
  127.                         onPressed: subtractClicked,
  128.                         child: Text(
  129.                           '-',
  130.                           style: TextStyle(color: Colors.white, fontSize: 20),
  131.                         ),
  132.                       ),
  133.                     ),
  134.                     SizedBox(
  135.                       width: 20,
  136.                     ),
  137.                     Expanded(
  138.                       child: RaisedButton(
  139.                         color: Colors.blue,
  140.                         onPressed: multiplyClicked,
  141.                         child: Text(
  142.                           '*',
  143.                           style: TextStyle(color: Colors.white, fontSize: 20),
  144.                         ),
  145.                       ),
  146.                     )
  147.                   ],
  148.                 ),
  149.                 SizedBox(
  150.                   height: 20,
  151.                 ),
  152.                 Row(
  153.                   children: [
  154.                     Expanded(
  155.                       child: RaisedButton(
  156.                         color: Colors.blue,
  157.                         onPressed: dividedClicked,
  158.                         child: Text(
  159.                           '/',
  160.                           style: TextStyle(color: Colors.white, fontSize: 20),
  161.                         ),
  162.                       ),
  163.                     ),
  164.                     SizedBox(
  165.                       width: 20,
  166.                     ),
  167.                     Expanded(
  168.                       child: RaisedButton(
  169.                         color: Colors.blue,
  170.                         onPressed: percentClicked,
  171.                         child: Text(
  172.                           '%',
  173.                           style: TextStyle(color: Colors.white, fontSize: 20),
  174.                         ),
  175.                       ),
  176.                     ),
  177.                     SizedBox(
  178.                       width: 20,
  179.                     ),
  180.                     Expanded(
  181.                       child: RaisedButton(
  182.                         color: Colors.green,
  183.                         onPressed: clearText,
  184.                         child: Text(
  185.                           'Clear',
  186.                           style: TextStyle(color: Colors.white, fontSize: 20),
  187.                         ),
  188.                       ),
  189.                     )
  190.                   ],
  191.                 ),
  192.               ],
  193.             ),
  194.           ),
  195.         ),
  196.       ),
  197.     );
  198.   }
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement