Last active
June 4, 2020 18:58
-
-
Save ogahga/6f6d9cad3beb1693046f42db5643852b to your computer and use it in GitHub Desktop.
This file contains 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'; | |
//TODO: Step 2 - Import the rFlutter_Alert package here. | |
import 'package:rflutter_alert/rflutter_alert.dart'; | |
import 'quiz_brain.dart'; | |
QuizBrain quizBrain = QuizBrain(); | |
void main() => runApp(Quizzler()); | |
class Quizzler extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
backgroundColor: Colors.grey.shade900, | |
body: SafeArea( | |
child: Padding( | |
padding: EdgeInsets.symmetric(horizontal: 10.0), | |
child: QuizPage(), | |
), | |
), | |
), | |
); | |
} | |
} | |
class QuizPage extends StatefulWidget { | |
@override | |
_QuizPageState createState() => _QuizPageState(); | |
} | |
class _QuizPageState extends State<QuizPage> { | |
List<Icon> scoreKeeper = []; | |
void checkAnswer(bool userPickedAnswer) { | |
bool correctAnswer = quizBrain.getCorrectAnswer(); | |
setState(() { | |
if (quizBrain.isFinished() == true) { | |
Alert( | |
context: context, | |
title: 'Finished!', | |
desc: 'You\'ve reached the end of the quiz.', | |
).show(); | |
quizBrain.reset(); | |
scoreKeeper = []; | |
} else { | |
if (userPickedAnswer == correctAnswer) { | |
scoreKeeper.add(Icon( | |
Icons.check, | |
color: Colors.green, | |
)); | |
} else { | |
scoreKeeper.add(Icon( | |
Icons.close, | |
color: Colors.red, | |
)); | |
} | |
quizBrain.nextQuestion(); | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
Expanded( | |
flex: 5, | |
child: Padding( | |
padding: EdgeInsets.all(10.0), | |
child: Center( | |
child: Text( | |
quizBrain.getQuestionText(), | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 25.0, | |
color: Colors.white, | |
), | |
), | |
), | |
), | |
), | |
Expanded( | |
child: Padding( | |
padding: EdgeInsets.all(15.0), | |
child: FlatButton( | |
textColor: Colors.white, | |
color: Colors.green, | |
child: Text( | |
'True', | |
style: TextStyle( | |
color: Colors.white, | |
fontSize: 20.0, | |
), | |
), | |
onPressed: () { | |
//The user picked true. | |
checkAnswer(true); | |
}, | |
), | |
), | |
), | |
Expanded( | |
child: Padding( | |
padding: EdgeInsets.all(15.0), | |
child: FlatButton( | |
color: Colors.red, | |
child: Text( | |
'False', | |
style: TextStyle( | |
fontSize: 20.0, | |
color: Colors.white, | |
), | |
), | |
onPressed: () { | |
//The user picked false. | |
checkAnswer(false); | |
}, | |
), | |
), | |
), | |
Row( | |
children: scoreKeeper, | |
) | |
], | |
); | |
} | |
} | |
/* | |
question1: 'You can lead a cow down stairs but not up stairs.', false, | |
question2: 'Approximately one quarter of human bones are in the feet.', true, | |
question3: 'A slug\'s blood is green.', true, | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment