skip to Main Content

I want correct answer to displays when the user selects the wrong answer
In this code, it displays the wrong answer only and does not display the correct answer. How do I make the code display the wrong and correct answer to the user together?

Color getColorForOption(Answer answer) {
    bool correctAnswer = answer.isCorrect;
    bool isSelected = answer == selecetedAnswer;
    if (isSelected) {
        return correctAnswer ? Colors.green : Colors.red;
     } 
      return Colors.grey.shade300;
    }

enter image description here

I tried this code but it didn’t work

Color getColorForOption(Answer answer) {
    bool correctAnswer = answer.isCorrect;
    bool isSelected = answer == selecetedAnswer;
    if (isSelected) {
      return correctAnswer ? Colors.green : Colors.red;
    } else if (correctAnswer) {
      return Colors.green;
    }
    return Colors.grey.shade300;
  }

2

Answers


  1. Chosen as BEST ANSWER

    I found the appropriate code for my question. Look at this code. Displays the correct answer with the wrong answer. If the user chooses the wrong answer, thank you.

    Color getColorForOption(Answer answer) {
    bool correctAnswer = answer.isCorrect;
    bool isSelected = answer == selectedAnswer; 
    if (isSelected) {
      return correctAnswer
          ? Colors.green
          : Colors.red; // Correct answer and selected
    }
    else if (!isSelected && correctAnswer && selectedAnswer != null) {
      return Colors.green; // Correct answer and not selected (show it when 
                              another option is selected)
    } else {
      return Colors.grey.shade300; // Wrong answer and not selected
    }
    

    }


  2. To achieve your aim, you have to approach the if-else statement in this manner.

    Color getColorForOption(Answer answer) {
        bool correctAnswer = answer.isCorrect;
        bool isSelected = answer == selecetedAnswer;
        if (isSelected) {
            return correctAnswer ? Colors.green : Colors.red;
        } else {
            return correctAnswer ? Colors.green : Colors.grey.shade300;
        }
    }
    

    So when the option is not correct, the unselected color will be green while the selected will be red.

    I already tested this and it worked.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search