Skip to content

Instantly share code, notes, and snippets.

@mampersat
Last active March 11, 2020 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mampersat/854c924592978b67d0b111423be9519e to your computer and use it in GitHub Desktop.
Save mampersat/854c924592978b67d0b111423be9519e to your computer and use it in GitHub Desktop.
Color Game
// Color game by M@
// 2020-03-10
// Minimul formatting... next thing to learn
// Haven't figured out how to execute a callback with a parameter
// Dunno how to call the spin() function ONCE on startup
import 'package:flutter/material.dart';
import 'dart:math' show Random;
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: true,
home: MyHomePage());
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _correct = 0;
int _incorrect = 0;
static var _colorNames = ["Red", "Blue", "Green", "Yellow"];
static var _colorValues = [
Colors.red,
Colors.blue,
Colors.green,
Colors.yellow
];
// initial state = Red text, Red Color... weak
String _colorName = _colorNames[2];
Color _colorValue = _colorValues[3];
int _rcn = 2;
int _rcv = 3;
void _spin() {
var random = Random.secure();
_rcn = random.nextInt(4);
_rcv = random.nextInt(4);
_colorName = _colorNames[_rcn];
_colorValue = _colorValues[_rcv];
}
void _clickRed() {
_click(0);
}
void _clickBlue() {
_click(1);
}
void _clickGreen() {
_click(2);
}
void _clickYellow() {
_click(3);
}
void _click(int i) {
setState(() {
if (i == _rcv) { // user clicked correct color
_correct++;
_spin();
} else { // user clicked wrong color
_incorrect++;
}
});
}
@override
Widget build(BuildContext context) {
return Column(children: [
Text('Click COLOR of next word'),
Text(_colorName, style: TextStyle(color: _colorValue)),
// Better would be to loop through colors.. and not have a diff function for each
Row(children: [
ColorButton(t: 'Red', onPressed: _clickRed),
ColorButton(t: 'Blue', onPressed: _clickBlue),
ColorButton(t: 'Yellow', onPressed: _clickYellow),
ColorButton(t: 'Green', onPressed: _clickGreen),
]),
Text('Correct: ' + _correct.toString()),
Text('Wrong: ' + _incorrect.toString()),
]);
}
}
class ColorButton extends StatelessWidget {
const ColorButton({Key key, this.t, this.onPressed}) : super(key: key);
final String t;
final Function onPressed;
@override
Widget build(BuildContext context) {
return Container(
child: RaisedButton(
child: Text(t),
onPressed: onPressed,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment