Skip to content

Instantly share code, notes, and snippets.

@Meghatronics
Last active May 3, 2020 13:07
Show Gist options
  • Select an option

  • Save Meghatronics/65fd5d677c6623ae32518cb8108d9b92 to your computer and use it in GitHub Desktop.

Select an option

Save Meghatronics/65fd5d677c6623ae32518cb8108d9b92 to your computer and use it in GitHub Desktop.
Day 3 30DaysOfCode May2020 Mobile Beginner MajorE
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