Created
December 19, 2022 08:58
-
-
Save RobbiNespu/8668c6ff16ec3c61af75f4b5ebb33ab4 to your computer and use it in GitHub Desktop.
multi choice quiz
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
class QuizPage extends StatefulWidget { | |
@override | |
_QuizPageState createState() => _QuizPageState(); | |
} | |
class _QuizPageState extends State<QuizPage> { | |
int _currentQuestionIndex = 0; | |
List<QuizQuestion> _questions = [ QuizQuestion( question: 'What is the capital of France?', answers: ['Paris', 'London', 'New York', 'Tokyo'], | |
correctAnswer: 'Paris', | |
), | |
QuizQuestion( | |
question: 'What is the tallest mountain in the world?', | |
answers: ['Mount Everest', 'K2', 'Kilimanjaro', 'Annapurna'], | |
correctAnswer: 'Mount Everest', | |
), | |
// more quiz questions... | |
]; | |
Map<String, bool> _answers = {}; | |
void _submitAnswers() { | |
int numCorrect = 0; | |
_questions.forEach((question) { | |
if (_answers[question.question] == question.correctAnswer) { | |
numCorrect++; | |
} | |
}); | |
// show the results | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Column( | |
children: [ | |
Text(_questions[_currentQuestionIndex].question), | |
..._questions[_currentQuestionIndex].answers.map((answer) { | |
return RadioListTile( | |
value: answer, | |
title: Text(answer), | |
groupValue: _answers[_questions[_currentQuestionIndex].question], | |
onChanged: (newValue) { | |
setState(() { | |
_answers[_questions[_currentQuestionIndex].question] = newValue; | |
}); | |
}, | |
); | |
}), | |
RaisedButton( | |
onPressed: _currentQuestionIndex < _questions.length - 1 | |
? () { | |
setState(() { | |
_currentQuestionIndex++; | |
}); | |
} | |
: _submitAnswers, | |
child: Text(_currentQuestionIndex < |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment