Last active
May 3, 2020 13:07
-
-
Save Meghatronics/65fd5d677c6623ae32518cb8108d9b92 to your computer and use it in GitHub Desktop.
Day 3 30DaysOfCode May2020 Mobile Beginner MajorE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp( | |
| MaterialApp( | |
| home: Scaffold( | |
| body: BarnCounter(), | |
| ), | |
| ), | |
| ); | |
| } | |
| class BarnCounter extends StatefulWidget { | |
| BarnCounterState createState() => BarnCounterState(); | |
| } | |
| class BarnCounterState extends State<BarnCounter> { | |
| int numberOfHeads = 0; | |
| int numberOfLegs = 0; | |
| String output = "Input your head and leg values and click 'Count Your Barn'"; | |
| //<MAIN CALCULATION FUNCTION IS HERE> | |
| void countTheBarn() { | |
| int chickensInTheBarn = ((4 * numberOfHeads) - numberOfLegs) ~/ 2; | |
| int goatsInTheBarn = (numberOfHeads - chickensInTheBarn); | |
| //</MAIN CALCULATION FUNCTION IS HERE> | |
| setState(() { | |
| if (chickensInTheBarn < 0 || goatsInTheBarn < 0) | |
| output = 'You may have counted wrong. Cannot have negative animals'; | |
| else | |
| output = | |
| 'You have $goatsInTheBarn goats and $chickensInTheBarn chickens'; | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Column(children: <Widget>[ | |
| Text('Here! Solve your barn count problem'), | |
| Row(children: <Widget>[ | |
| Expanded( | |
| flex: 3, | |
| child: TextField( | |
| onChanged: (value) { | |
| numberOfHeads = int.parse(value); | |
| }, | |
| decoration: InputDecoration(labelText: 'Enter Number of HEADS'), | |
| ), | |
| ), | |
| Spacer(), | |
| Expanded( | |
| flex: 3, | |
| child: TextField( | |
| onChanged: (value) { | |
| numberOfLegs = int.parse(value); | |
| }, | |
| decoration: InputDecoration(labelText: 'Enter Number of LEGS'), | |
| ), | |
| ) | |
| ]), | |
| FlatButton(child: Text('Count Your Barn'), onPressed: countTheBarn), | |
| Text(output, style: TextStyle(fontSize: 20)) | |
| ]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment