A simple quiz application built using Flutter (version 3.27.1) and the dio
package for fetching quiz data from a REST API. The app allows users to answer multiple-choice questions, calculates their score, and displays a result at the end using the quickalert
package.
- Fetches quiz data from a REST API.
- Displays questions and multiple-choice answers.
- Validates answers and calculates scores.
- Provides feedback for each question (Correct/Incorrect).
- Shows a completion dialog with the final score.
- Flutter SDK:
3.27.1
- Dart:
>=2.19.0 <4.0.0
Below are the dependencies used in the project:
dependencies:
flutter:
sdk: flutter
dio: ^5.3.1
quickalert: ^2.1.0
-
Clone the repository:
git clone <repository-url>
-
Navigate to the project directory:
cd quiz_app
-
Get the required dependencies:
flutter pub get
-
Run the app:
flutter run
QuizPage
: The main screen that handles the quiz flow.QuizData
: A model to parse quiz data from the API.Question
: A model to handle individual questions.Option
: A model for multiple-choice options.
The app fetches quiz data from the API endpoint:
final url = 'https://api.jsonserve.com/Uw5CrX';
final response = await dio.get(url);
The quickalert
package is used to display the result at the end of the quiz:
QuickAlert.show(
context: context,
type: QuickAlertType.success,
title: 'Quiz Completed!',
text: 'Your final score is: \$score',
confirmBtnText: 'Restart',
onConfirmBtnTap: () {
Navigator.of(context).pop();
setState(() {
currentQuestionIndex = 0;
score = 0;
selectedAnswer = null;
showResult = false;
});
},
);
Add screenshots of your app here (e.g., main screen, question page, completion dialog).
void submitAnswer(Question question) {
setState(() {
final correctOption = question.options.firstWhere((opt) => opt.isCorrect);
if (selectedAnswer == correctOption.description) {
score += 4;
} else {
score += -1;
}
showResult = true;
});
}
void nextQuestion(QuizData quizData) {
setState(() {
if (currentQuestionIndex < quizData.questions.length - 1) {
currentQuestionIndex++;
selectedAnswer = null;
showResult = false;
} else {
QuickAlert.show(
context: context,
type: QuickAlertType.success,
title: 'Quiz Completed!',
text: 'Your final score is: \$score',
confirmBtnText: 'Restart',
onConfirmBtnTap: () {
Navigator.of(context).pop();
setState(() {
currentQuestionIndex = 0;
score = 0;
selectedAnswer = null;
showResult = false;
});
},
);
}
});
}
This project is open-source and available under the MIT License.
Happy coding!