You are on page 1of 4

import 'package:flutter/cupertino.

dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: "Calculator",
    home: HomePage1(),
    theme: ThemeData(
      primarySwatch: Colors.teal,
    ),
  ));
}

class HomePage1 extends StatefulWidget {


  const HomePage1({Key? key}) : super(key: key);

  @override
  _HomePage1State createState() => _HomePage1State();
}

class _HomePage1State extends State<HomePage1> {


  int firstnumber = 0;
  int sceondnumber = 0;
  String textdisply = "";
  String result = "";
  String operation = "";

  void buttonClick(String bt) {


    if (bt == "C") {
      firstnumber = 0;
      sceondnumber = 0;
      textdisply = "";
      result = "";
    } else if (bt == '%' || bt == 'x' || bt == '+' || bt == '-') {
      firstnumber = int.parse(textdisply);
      result = "";
      operation = bt;
    } else if (bt == '=') {
      sceondnumber = int.parse(textdisply);
      if (operation == '+') {
        result = (firstnumber + sceondnumber).toString();
      }

      if (operation == '-') {
        result = (firstnumber - sceondnumber).toString();
      }

      if (operation == 'x') {
        result = (firstnumber * sceondnumber).toString();
      }

      if (operation == '%') {
        result = (firstnumber % sceondnumber).toString();
      } else
        result = int.parse(textdisply + bt).toString();
    }
    setState(() {
      textdisply = result;
    });
  }

  Widget button(String bt) {


    return Expanded(
      child: OutlineButton(
        onPressed: () => buttonClick(bt),
        padding: EdgeInsets.all(22.0),
        child: Text(
          "$bt",
          style: TextStyle(fontSize: 25.0, fontWeight: FontWeight.bold),
        ),
        color: Colors.blueGrey,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(child: Text("Calculator..")),
      ),
      body: Container(
        child: Padding(
          padding: const EdgeInsets.all(18.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Expanded(
                child: Container(
                  child: Padding(
                    padding: EdgeInsets.all(30.0),
                    child: Text(
                      "$textdisply",
                      style: TextStyle(fontSize: 38),
                    ),
                  ),
                  alignment: Alignment.bottomRight,
                ),
              ),
              Row(
                children: [
                  button("9"),
                  button("8"),
                  button("7"),
                  button("%"),
                ],
              ),
              Row(
                children: [
                  button("6"),
                  button("5"),
                  button("4"),
                  button("x"),
                ],
              ),
              Row(
                children: [
                  button("3"),
                  button("2"),
                  button("1"),
                  button("+"),
                ],
              ),
              Row(
                children: [
                  button("C"),
                  button("0"),
                  button("="),
                  button("-"),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

You might also like