From 7f71b299f1a27c9f7f86d79f67b589c4df21a28c Mon Sep 17 00:00:00 2001 From: ananfang Date: Fri, 17 May 2019 18:58:35 +0800 Subject: [PATCH] let the user know the correct answer about the last quesiton. --- lib/main.dart | 4 ++-- lib/quiz_brain.dart | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 4f6bbfe..35d61b5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -33,8 +33,6 @@ class _QuizPageState extends State { List scoreKeeper = []; void checkAnswer(bool userPickedAnswer) { - bool correctAnswer = quizBrain.getCorrectAnswer(); - setState(() { //TODO: Step 4 - Use IF/ELSE to check if we've reached the end of the quiz. If so, //On the next line, you can also use if (quizBrain.isFinished()) {}, it does the same thing. @@ -60,6 +58,8 @@ class _QuizPageState extends State { //TODO: Step 6 - If we've not reached the end, ELSE do the answer checking steps below 👇 else { + bool correctAnswer = quizBrain.getCorrectAnswer(); + if (userPickedAnswer == correctAnswer) { scoreKeeper.add(Icon( Icons.check, diff --git a/lib/quiz_brain.dart b/lib/quiz_brain.dart index 68e1339..1b5b4d0 100644 --- a/lib/quiz_brain.dart +++ b/lib/quiz_brain.dart @@ -34,11 +34,17 @@ class QuizBrain { void nextQuestion() { if (_questionNumber < _questionBank.length - 1) { _questionNumber++; + } else { + _questionNumber = -1; } } String getQuestionText() { - return _questionBank[_questionNumber].questionText; + if (isFinished()) { + return 'Finished!\nTap any button to restart.'; + } else { + return _questionBank[_questionNumber].questionText; + } } bool getCorrectAnswer() { @@ -48,7 +54,7 @@ class QuizBrain { //TODO: Step 3 Part A - Create a method called isFinished() here that checks to see if we have reached the last question. It should return (have an output) true if we've reached the last question and it should return false if we're not there yet. bool isFinished() { - if (_questionNumber >= _questionBank.length - 1) { + if (_questionNumber == -1) { //TODO: Step 3 Part B - Use a print statement to check that isFinished is returning true when you are indeed at the end of the quiz and when a restart should happen. print('Now returning true');