Skip to content

Instantly share code, notes, and snippets.

@zontyp
Created August 31, 2020 15:53
Show Gist options
  • Save zontyp/10cd08e1b6d4ca266c8d8b4afe68091b to your computer and use it in GitHub Desktop.
Save zontyp/10cd08e1b6d4ca266c8d8b4afe68091b to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(new GuessNumberGame());
}
class GuessNumberGame extends StatefulWidget {
@override
_GuessNumberState createState() => _GuessNumberState();
}
class _GuessNumberState extends State<GuessNumberGame> {
@override
int computer ;
String message;
@override
void initState() {
super.initState();
Random r = Random ();
computer = r.nextInt(10);
computer = computer + 1;
message = "Guess my number :)";
}
void calculateResult(int player){
if (computer > player)
{
setState(() {
message = "My number is higher.";
});
}
if (computer < player)
{
setState(() {
message = "My number is lower.";
});
}
if (computer == player)
{
setState(() {
message = "You got it !";
Random r = Random ();
computer = r.nextInt(10);
computer = computer + 1;
});
}
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
// backgroundColor: Colors.blueGrey,
body:
Container(
margin: const EdgeInsets.fromLTRB(0, 0, 0, 0),
padding: EdgeInsets.all(11.0),
color: Colors.black87,
child:Column(
children:[
Container(
margin: const EdgeInsets.fromLTRB(0, 100, 0, 30),
child:Text(
"$message",
style:TextStyle(color: Colors.lightGreenAccent,fontSize: 22),
),
),
Row(
children:[
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(1);
},
child: Text(
"1",
style: TextStyle(fontSize: 32.0),
),
),
),
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(2);
},
child: Text(
"2",
style: TextStyle(fontSize: 32.0),
),
),
),
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(3);
},
child: Text(
"3",
style: TextStyle(fontSize: 32.0),
),
),
),
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(4);
},
child: Text(
"4",
style: TextStyle(fontSize: 32.0),
),
),
),
]
),
Row(
children:[
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(5);
},
child: Text(
"5",
style: TextStyle(fontSize: 32.0),
),
),
),
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(6);
},
child: Text(
"6",
style: TextStyle(fontSize: 32.0),
),
),
),
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(7);
},
child: Text(
"7",
style: TextStyle(fontSize: 32.0),
),
),
),
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(8);
},
child: Text(
"8",
style: TextStyle(fontSize: 32.0),
),
),
),
]
),
Row(
children:[
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(9);
},
child: Text(
"9",
style: TextStyle(fontSize: 32.0),
),
),
),
Container(
width: 70,
height:60,
margin: const EdgeInsets.fromLTRB(0, 0, 10, 10),
child:FlatButton(
color: Colors.black,
textColor: Colors.lightGreenAccent,
onPressed: () {
calculateResult(10);
},
child: Text(
"10",
style: TextStyle(fontSize: 32.0),
),
),
),
]
),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment