What's new

Help Bot help flutter

loaderdiper

Forum Veteran
Joined
Mar 21, 2016
Posts
1,657
Solutions
46
Reaction
2,360
Points
703
How to create calculator code in flutter?
 
To create a simple calculator in Flutter, you can start by:

1. Creating a new Flutter project in your IDE or editor of choice.
2. Open the main.dart file and remove the default code.
3. Import the material.dart package for material design widgets.
4. Create a new StatefulWidget named Calculator with the default state.
5. In the build method of the Calculator widget, create a Column widget.
6. Inside the Column widget, create a Text widget to display the calculator output.
7. Below the Text widget, create a Row widget to hold the calculator buttons.
8. Inside the Row widget, create a Column widget for each row of buttons.
9. Inside each Column widget, create a FlatButton widget for each button.
10. Add the appropriate logic to each button to perform the desired operation when pressed.

Here is an example code snippet to get you started:

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

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

class Calculator extends StatefulWidget {
  @override
  _CalculatorState createState() => _CalculatorState();
}

class _CalculatorState extends State<Calculator> {
  String output = "0";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Calculator"),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              output,
              style: TextStyle(fontSize: 24),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Column(
                  children: [
                    FlatButton(
                      onPressed: () {},
                      child: Text("7"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("4"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("1"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("0"),
                    ),
                  ],
                ),
                Column(
                  children: [
                    FlatButton(
                      onPressed: () {},
                      child: Text("8"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("5"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("2"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("."),
                    ),
                  ],
                ),
                Column(
                  children: [
                    FlatButton(
                      onPressed: () {},
                      child: Text("9"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("6"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("3"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("="),
                    ),
                  ],
                ),
                Column(
                  children: [
                    FlatButton(
                      onPressed: () {},
                      child: Text("+"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("-"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("*"),
                    ),
                    FlatButton(
                      onPressed: () {},
                      child: Text("/"),
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
Note that the logic for performing the calculations is not included in this example, and you will need to add that yourself based on your specific requirements.
 

Similar threads

Back
Top