quiz screen in flutter

import 'package:flutter/material.dart';


void main() {

  runApp(QuizApp());

}


class QuizApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title: 'Quiz App',

      theme: ThemeData(

        primarySwatch: Colors.blue,

      ),

      home: QuizScreen(),

    );

  }

}


class QuizScreen extends StatefulWidget {

  @override

  _QuizScreenState createState() => _QuizScreenState();

}


class _QuizScreenState extends State<QuizScreen> {

  List<Question> _questions = [

    Question('Question 1', true),

    Question('Question 2', false),

    Question('Question 3', true),

  ];

  int _currentQuestionIndex = 0;

  bool _showResult = false;

  List<bool> _userAnswers = [];


  void _answerQuestion(bool answer) {

    _userAnswers.add(answer);

    if (_currentQuestionIndex < _questions.length - 1) {

      setState(() {

        _currentQuestionIndex++;

      });

    } else {

      setState(() {

        _showResult = true;

      });

    }

  }


  void _restartQuiz() {

    setState(() {

      _currentQuestionIndex = 0;

      _showResult = false;

      _userAnswers.clear();

    });

  }


  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text('Quiz App'),

      ),

      body: _showResult ? _buildResultScreen() : _buildQuestionScreen(),

    );

  }


  Widget _buildQuestionScreen() {

    return Column(

      mainAxisAlignment: MainAxisAlignment.center,

      children: [

        Text(

          'Question ${_currentQuestionIndex + 1}',

          style: TextStyle(

            fontSize: 24,

            fontWeight: FontWeight.bold,

          ),

        ),

        SizedBox(height: 20),

        Text(

          _questions[_currentQuestionIndex].questionText,

          style: TextStyle(fontSize: 18),

        ),

        SizedBox(height: 20),

        Row(

          mainAxisAlignment: MainAxisAlignment.center,

          children: [

            ElevatedButton(

              onPressed: () => _answerQuestion(true),

              child: Text('True'),

            ),

            SizedBox(width: 20),

            ElevatedButton(

              onPressed: () => _answerQuestion(false),

              child: Text('False'),

            ),

          ],

        ),

      ],

    );

  }


  Widget _buildResultScreen() {

    int correctAnswers = 0;

    for (int i = 0; i < _questions.length; i++) {

      if (_userAnswers[i] == _questions[i].correctAnswer) {

        correctAnswers++;

      }

    }


    return Column(

      mainAxisAlignment: MainAxisAlignment.center,

      children: [

        Text(

          'Quiz Result',

          style: TextStyle(

            fontSize: 24,

            fontWeight: FontWeight.bold,

          ),

        ),

        SizedBox(height: 20),

        Text(

          'Correct Answers: $correctAnswers/${_questions.length}',

          style: TextStyle(fontSize: 18),

        ),

        SizedBox(height: 20),

        ElevatedButton(

          onPressed: _restartQuiz,

          child: Text('Restart Quiz'),

        ),

      ],

    );

  }

}


class Question {

  final String questionText;

  final bool correctAnswer;


  Question(this.questionText, this.correctAnswer);

}


Comments

Popular posts from this blog

swapping the alternate values in given array's element ! 17/11/2022

Learning stage | c++ programs

c++ basic question