zeba_academy_quiz_engine 1.0.0 copy "zeba_academy_quiz_engine: ^1.0.0" to clipboard
zeba_academy_quiz_engine: ^1.0.0 copied to clipboard

Powerful offline quiz engine with analytics.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:zeba_academy_quiz_engine/zeba_academy_quiz_engine.dart';

void main() {
  runApp(const QuizExampleApp());
}

class QuizExampleApp extends StatelessWidget {
  const QuizExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: QuizHomePage(),
    );
  }
}

class QuizHomePage extends StatefulWidget {
  const QuizHomePage({super.key});

  @override
  State<QuizHomePage> createState() => _QuizHomePageState();
}

class _QuizHomePageState extends State<QuizHomePage> {
  late QuizEngine engine;
  String? selectedAnswer;

  @override
  void initState() {
    super.initState();

    final questions = [
      QuizQuestion(
        id: "1",
        question: "Capital of France?",
        type: QuestionType.mcq,
        options: ["Paris", "London", "Rome"],
        correctAnswer: "Paris",
        marks: 2,
      ),
      QuizQuestion(
        id: "2",
        question: "Flutter is developed by Google.",
        type: QuestionType.trueFalse,
        options: ["True", "False"],
        correctAnswer: "True",
      ),
      QuizQuestion(
        id: "3",
        question: "2 + 2 = ?",
        type: QuestionType.fillBlank,
        correctAnswer: "4",
      ),
    ];

    engine = QuizEngine(questions);
    engine.startTimer(() {
      setState(() {});
    });
  }

  void submitAndNext() {
    if (selectedAnswer != null) {
      engine.submitAnswer(selectedAnswer!);
    }

    if (engine.nextQuestion()) {
      setState(() {
        selectedAnswer = null;
      });
    } else {
      final result = engine.finishQuiz();
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (_) => QuizResultScreen(result: result),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final question = engine.currentQuestion;

    return Scaffold(
      appBar: AppBar(
        title: const Text("Zeba Academy Quiz Engine"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "Question ${engine.currentIndex + 1}",
              style: const TextStyle(fontSize: 18),
            ),
            const SizedBox(height: 10),
            Text(
              question.question,
              style: const TextStyle(fontSize: 20),
            ),
            const SizedBox(height: 20),

            if (question.type != QuestionType.fillBlank)
              ...?question.options?.map(
                    (option) => RadioListTile<String>(
                  title: Text(option),
                  value: option,
                  groupValue: selectedAnswer,
                  onChanged: (value) {
                    setState(() {
                      selectedAnswer = value;
                    });
                  },
                ),
              ),

            if (question.type == QuestionType.fillBlank)
              TextField(
                decoration: const InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: "Enter your answer",
                ),
                onChanged: (value) {
                  selectedAnswer = value;
                },
              ),

            const Spacer(),

            Text(
              "Time: ${engine.elapsed.inSeconds} sec",
              style: const TextStyle(fontSize: 16),
            ),

            const SizedBox(height: 10),

            SizedBox(
              width: double.infinity,
              child: ElevatedButton(
                onPressed: submitAndNext,
                child: const Text("Next"),
              ),
            )
          ],
        ),
      ),
    );
  }
}
0
likes
130
points
90
downloads

Documentation

API reference

Publisher

verified publisherzeba.academy

Weekly Downloads

Powerful offline quiz engine with analytics.

Homepage

License

unknown (license)

Dependencies

flutter, shared_preferences, uuid

More

Packages that depend on zeba_academy_quiz_engine