londonappbrewery / quizzler-flutter-challenge-final

Completed version of the Quizzler project in the Complete Flutter Bootcamp
https://www.appbrewery.co
63 stars 137 forks source link

Last answer is not Displayed #6

Open navdeepblock8 opened 4 years ago

navdeepblock8 commented 4 years ago

The scorekeeper resets before the last answer is displayed.

pradeep9201439460 commented 4 years ago

Instead of Row( children: scoreKeeper, ),

use this one Wrap( children: scoreKeeper, ),

pranjalg13 commented 4 years ago

Just add this additional function in main.dart so it will delay the code execution.

` void checkAns(bool ans) { setState(() { if (quizbrain.isFinished() == true) { if (quizbrain.getanswer() == ans) { myiconList.add(Icon( Icons.check, color: Colors.green, )); } else { myiconList.add(Icon( Icons.close, color: Colors.red, )); } reset(); } else { if (quizbrain.getanswer() == ans) { myiconList.add(Icon( Icons.check, color: Colors.green, )); } else { myiconList.add(Icon( Icons.close, color: Colors.red, )); } quizbrain.nextQuestion(); } }); }

void reset() async { await new Future.delayed(const Duration(seconds: 1)); setState(() { Alert( context: context, title: "QUIZ OVER", desc: "Thanks for taking quiz") .show(); quizbrain.resetQuestionNumber(); myiconList = []; }); } `

infinite-being commented 3 years ago

Inspired by @pranjalg13 Here is the more elegant way of doing it: Add the following code inside your 'Quiz Brain' file:

class QuizBrain {
  int _questionNumber = 0;
  bool _isReachedLastQuestion = false;
  List<Question> _questions = [
 //Your Questions List
  ];
  void nextQuestion() {
    if (_questionNumber < _questions.length - 1) {
      _questionNumber++;
    }
  }

  String getTheQuestion() {
    return _questions[_questionNumber].theQuestion;
  }

  bool getTheAnswer() {
    return _questions[_questionNumber].theAnswer;
  }

  bool isLastQuestion() {
    if (_questionNumber == _questions.length - 1) {
      return true;
    }
    return false;
  }

  bool isFinished() {
    if (_isReachedLastQuestion) {
      return true;
    } else {
      return false;
    }
  }

  void reachedLastQuestion() {
    _isReachedLastQuestion = true;
  }

  void resetQuiz() {
    _questionNumber = 0;
  }
}

I have added a few more check named 'reachedLastQuestion()', ' _isReachedLastQuestion ' to check if we have reached the last question or not. And here is the code snippet for my 'checkAnswers()' function inside 'main' function:

void resetDialog() async {
    await new Future.delayed(const Duration(seconds: 1));
    Alert(
      context: context,
      type: AlertType.info,
      title: "Quiz Ended..!",
      desc: "Restart the Quiz?",
      buttons: [
        DialogButton(
          child: Text(
            "COOL",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          onPressed: () {
            setState(() {
              scoreKeeper = new List();
              quizBrain.resetQuiz();
              Navigator.pop(context);
            });
          },
          width: 120,
        )
      ],
    ).show();
  }

  void checkAnswers(bool userAnswer) {
    if (!quizBrain.isFinished()) {
      if (quizBrain.isLastQuestion()) {
        quizBrain.reachedLastQuestion();
      }
      bool correctAnswer = quizBrain.getTheAnswer();
      if (correctAnswer == userAnswer) {
        scoreKeeper.add(
          Icon(
            Icons.check,
            color: Colors.green,
          ),
        );
      } else {
        scoreKeeper.add(
          Icon(
            Icons.close,
            color: Colors.red,
          ),
        );
      }
      quizBrain.nextQuestion();
    }
    if (quizBrain.isFinished()) {
      resetDialog();
    }
  }

The code and the flow are simple and self-explanatory if you don't get something or want to give feedback then feel free to reply.

Wahab-Khan commented 3 years ago

instead of that code i just use this approach ...

bool isFinish = false;

  void nextQuestion() {
    if (questionNumber < _questionBank.length - 1) {
      questionNumber++;
    } else {
      isFinish = true;
    }
  }

bool isFinished() {
    return isFinish;
}

Now it's working fine ...