loaderdiper
Forum Veteran
- Joined
- Mar 21, 2016
- Posts
- 1,657
- Solutions
- 46
- Reaction
- 2,360
- Points
- 703
How to create calculator code in flutter?
material.dart
package for material design widgets.StatefulWidget
named Calculator
with the default state.build
method of the Calculator
widget, create a Column
widget.Column
widget, create a Text
widget to display the calculator output.Text
widget, create a Row
widget to hold the calculator buttons.Row
widget, create a Column
widget for each row of buttons.Column
widget, create a FlatButton
widget for each button.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("/"),
),
],
),
],
),
],
),
),
);
}
}