Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatefulWidget {
- @override
- _MyAppState createState() => _MyAppState();
- }
- class _MyAppState extends State<MyApp> {
- int firstNumber;
- int secondNumber;
- var result;
- TextEditingController firstTextEditingController;
- TextEditingController secondTextEditingController;
- @override
- void initState() {
- // TODO: implement initState
- super.initState();
- firstTextEditingController = TextEditingController();
- secondTextEditingController = TextEditingController();
- }
- void changeFirstNumber(String str) {
- print(str);
- firstTextEditingController.text = str;
- }
- void changeSecondNumber(String str) {
- print(str);
- secondTextEditingController.text = str;
- }
- void plusClicked() {
- result = int.parse(firstTextEditingController.text) +
- int.parse(secondTextEditingController.text);
- setState(() {});
- }
- void subtractClicked() {
- result = int.parse(firstTextEditingController.text) -
- int.parse(secondTextEditingController.text);
- setState(() {});
- }
- void multiplyClicked() {
- result = int.parse(firstTextEditingController.text) *
- int.parse(secondTextEditingController.text);
- setState(() {});
- }
- void dividedClicked() {
- result = int.parse(firstTextEditingController.text) /
- int.parse(secondTextEditingController.text);
- setState(() {});
- }
- void percentClicked() {
- result = int.parse(firstTextEditingController.text) %
- int.parse(secondTextEditingController.text);
- setState(() {});
- }
- void clearText() {
- firstTextEditingController.text = '';
- secondTextEditingController.text = '';
- result = '';
- setState(() {});
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Calculator Homework',
- home: Scaffold(
- appBar: AppBar(
- title: Text('Calculator Homework'),
- ),
- body: Center(
- child: Container(
- padding: EdgeInsets.all(30),
- child: Column(
- children: [
- // first input box
- TextField(
- controller: firstTextEditingController,
- keyboardType: TextInputType.number,
- onSubmitted: changeFirstNumber,
- ),
- SizedBox(
- height: 20,
- ),
- // second input box
- TextField(
- controller: secondTextEditingController,
- keyboardType: TextInputType.number,
- onSubmitted: changeSecondNumber,
- ),
- SizedBox(
- height: 20,
- ),
- //Result Text
- Text('Result : $result'),
- SizedBox(
- height: 20,
- ),
- Row(
- children: [
- Expanded(
- child: RaisedButton(
- color: Colors.blue,
- onPressed: plusClicked,
- child: Text(
- '+',
- style: TextStyle(color: Colors.white, fontSize: 20),
- ),
- ),
- ),
- SizedBox(
- width: 20,
- ),
- Expanded(
- child: RaisedButton(
- color: Colors.blue,
- onPressed: subtractClicked,
- child: Text(
- '-',
- style: TextStyle(color: Colors.white, fontSize: 20),
- ),
- ),
- ),
- SizedBox(
- width: 20,
- ),
- Expanded(
- child: RaisedButton(
- color: Colors.blue,
- onPressed: multiplyClicked,
- child: Text(
- '*',
- style: TextStyle(color: Colors.white, fontSize: 20),
- ),
- ),
- )
- ],
- ),
- SizedBox(
- height: 20,
- ),
- Row(
- children: [
- Expanded(
- child: RaisedButton(
- color: Colors.blue,
- onPressed: dividedClicked,
- child: Text(
- '/',
- style: TextStyle(color: Colors.white, fontSize: 20),
- ),
- ),
- ),
- SizedBox(
- width: 20,
- ),
- Expanded(
- child: RaisedButton(
- color: Colors.blue,
- onPressed: percentClicked,
- child: Text(
- '%',
- style: TextStyle(color: Colors.white, fontSize: 20),
- ),
- ),
- ),
- SizedBox(
- width: 20,
- ),
- Expanded(
- child: RaisedButton(
- color: Colors.green,
- onPressed: clearText,
- child: Text(
- 'Clear',
- style: TextStyle(color: Colors.white, fontSize: 20),
- ),
- ),
- )
- ],
- ),
- ],
- ),
- ),
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement