Skip to content

Sahulhameedu/Quiz-App-Flutter

Repository files navigation

Quiz App

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.

Features

  • 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.

Requirements

  • Flutter SDK: 3.27.1
  • Dart: >=2.19.0 <4.0.0

Dependencies

Below are the dependencies used in the project:

dependencies:
  flutter:
    sdk: flutter
  dio: ^5.3.1
  quickalert: ^2.1.0

Installation

  1. Clone the repository:

    git clone <repository-url>
  2. Navigate to the project directory:

    cd quiz_app
  3. Get the required dependencies:

    flutter pub get
  4. Run the app:

    flutter run

Usage

App Structure

  • 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.

Fetching Quiz Data

The app fetches quiz data from the API endpoint:

final url = 'https://api.jsonserve.com/Uw5CrX';
final response = await dio.get(url);

Showing Completion Dialog

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;
    });
  },
);

Screenshots

Add screenshots of your app here (e.g., main screen, question page, completion dialog).

Code Highlights

Submitting an Answer

void submitAnswer(Question question) {
  setState(() {
    final correctOption = question.options.firstWhere((opt) => opt.isCorrect);
    if (selectedAnswer == correctOption.description) {
      score += 4;
    } else {
      score += -1;
    }
    showResult = true;
  });
}

Navigation to the Next Question

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;
          });
        },
      );
    }
  });
}

Image

Image

Image

License

This project is open-source and available under the MIT License.


Happy coding!

About

A gamification quiz app

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published