Untitled

 avatar
unknown
plain_text
10 days ago
4.4 kB
3
Indexable
import 'package:flutter/material.dart';

void main() {
  runApp(CalculatorApp());
}

class CalculatorApp extends StatelessWidget {
  const CalculatorApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Calculator',
      theme: ThemeData(
        primaryColor: Colors.blue[400],
      ),
      home: CalculatorDisplay(),
    );
  }
}

class CalculatorDisplay extends StatefulWidget {
  const CalculatorDisplay({super.key});

  @override
  State<CalculatorDisplay> createState() => _CalculatorDisplayState();
}

class _CalculatorDisplayState extends State<CalculatorDisplay> {
  @override

  String output = "0";
  String _output = "0";
  double num1 = 0;
  double num2 = 0;
  String operand = "";

  buttonPressed(String buttonText) {
    if (buttonText == "C") {
      _output = "0";
      num1 = 0;
      num2 = 0;
      operand = "";
    } else if(buttonText == "+" || 
              buttonText == "-" ||
              buttonText == "×" ||
              buttonText == "÷" ) {
        num1 = double.parse(output);
        operand = buttonText;
        _output = "0";  
    } else if(buttonText == "=") {
      num2 = double.parse(output);

      switch(operand) {
        case "+" :
          _output = (num1 + num2).toString();
          break; 
        case "-" :
          _output = (num1 - num2).toString();
          break; 
        case "×" :
          _output = (num1 * num2).toString();
          break; 
        case "÷" :
          _output = num2 != 0 ? (num1 / num2).toString() : "Error";
          break; 
      }

      num1 = 0;
      num2 = 0;
      operand = "";
    } else {
      _output += buttonText;
    }

    setState(() {
      output = double.tryParse(_output)?.toString() ?? "0";
    });
  }

  Widget buildButton(String buttonText, Color color) {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: ElevatedButton(
          style: ElevatedButton.styleFrom(
            shape: CircleBorder(),
            padding: EdgeInsets.all(20.0),
            backgroundColor: color,
          ),
          onPressed: () => buttonPressed(buttonText),
          child: Text(
            buttonText,
            style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold, color: Colors.black),
          ),
        ),
      ),
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Calculator"),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Container(
              padding: const EdgeInsets.all(20.0),
              alignment: Alignment.centerRight,
              child: Text(
                output,
                style: TextStyle(
                  fontSize: 48.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.black,
                ),
              ),
            ),
          ),
          Column(
            children: [
              Row(
                children: [
                  buildButton("7", Colors.grey),
                  buildButton("8", Colors.grey),
                  buildButton("9", Colors.grey),
                  buildButton("÷", Colors.yellow),
                ],
              ),
              Row(
                children: [
                  buildButton("4", Colors.grey),
                  buildButton("5", Colors.grey),
                  buildButton("6", Colors.grey),
                  buildButton("×", Colors.yellow),
                ],
              ),
              Row(
                children: [
                  buildButton("1", Colors.grey),
                  buildButton("2", Colors.grey),
                  buildButton("3", Colors.grey),
                  buildButton("-", Colors.yellow),
                ],
              ),
              Row(
                children: [
                  buildButton("C", Colors.red),
                  buildButton("0", Colors.grey),
                  buildButton("=", Colors.green),
                  buildButton("+", Colors.yellow),
                ],
              ),
            ],
          )
        ],
      ),
    );
  }
}
Editor is loading...
Leave a Comment