-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Interactive Quiz in Explore Page #52 #87
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
39bc62f
Added Interactive Quiz in Explore Page
Prashant-2024 c2f1c6c
Merge branch 'master' into quiz
Prashant-2024 227b735
.gitignore updated for pubspec.lock
Prashant-2024 574dd73
Merge branch 'quiz' of https://github.com/Prashant-2024/learn into quiz
Prashant-2024 ace90fa
Delete pubspec.lock
Prashant-2024 2b0298a
Update greetings.yaml
Prashant-2024 2f3d99b
Revert "Update greetings.yaml"
Prashant-2024 8bcd4f5
Revert "Delete pubspec.lock"
Prashant-2024 7bca3d8
Delete pubspec.lock
sapatevaibhav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
description: This file stores settings for Dart & Flutter DevTools. | ||
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states | ||
extensions: |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'dart:math'; | ||
import 'package:flutter_svg/flutter_svg.dart'; | ||
import 'package:learn/utils/constants.dart'; | ||
|
||
class QuizQuestion { | ||
final String question; | ||
final List<String> options; | ||
final int correctAnswerIndex; | ||
|
||
QuizQuestion({ | ||
required this.question, | ||
required this.options, | ||
required this.correctAnswerIndex, | ||
}); | ||
} | ||
|
||
class QuizPage extends StatefulWidget { | ||
const QuizPage({super.key}); | ||
|
||
@override | ||
_QuizPageState createState() => _QuizPageState(); | ||
} | ||
|
||
class _QuizPageState extends State<QuizPage> { | ||
late List<QuizQuestion> _questions; | ||
int _currentQuestionIndex = 0; | ||
int _score = 0; | ||
bool _quizCompleted = false; | ||
String _feedbackMessage = ''; | ||
Color _feedbackColor = Colors.transparent; | ||
bool _answered = false; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_questions = List.from(AppConstants.quizQuestions); | ||
_questions.shuffle(); | ||
if (_questions.length > 10) { | ||
_questions = _questions.sublist(0, 10); | ||
} | ||
} | ||
|
||
void _answerQuestion(int selectedIndex) { | ||
if (_answered) return; | ||
|
||
bool isCorrect = | ||
selectedIndex == _questions[_currentQuestionIndex].correctAnswerIndex; | ||
setState(() { | ||
_answered = true; | ||
if (isCorrect) { | ||
_score++; | ||
_feedbackMessage = 'Correct! Good job!'; | ||
_feedbackColor = Colors.green; | ||
} else { | ||
_feedbackMessage = | ||
'Incorrect. The correct answer is: ${_questions[_currentQuestionIndex].options[_questions[_currentQuestionIndex].correctAnswerIndex]}'; | ||
_feedbackColor = Colors.red; | ||
} | ||
}); | ||
} | ||
|
||
void _nextQuestion() { | ||
setState(() { | ||
if (_currentQuestionIndex < _questions.length - 1) { | ||
_currentQuestionIndex++; | ||
_feedbackMessage = ''; | ||
_feedbackColor = Colors.transparent; | ||
_answered = false; | ||
} else { | ||
_quizCompleted = true; | ||
} | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Quiz'), | ||
), | ||
body: Padding( | ||
padding: const EdgeInsets.all(16.0), | ||
child: _quizCompleted | ||
? Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Text( | ||
'Quiz Completed! Your score is $_score/${_questions.length}', | ||
style: const TextStyle( | ||
fontSize: 24, fontWeight: FontWeight.bold), | ||
textAlign: TextAlign.center, | ||
), | ||
const SizedBox(height: 20), | ||
ElevatedButton( | ||
onPressed: () { | ||
Navigator.of(context).pop(); | ||
}, | ||
child: const Text('Back to Explore'), | ||
), | ||
], | ||
) | ||
: Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Text( | ||
'Question ${_currentQuestionIndex + 1}/${_questions.length}', | ||
style: const TextStyle( | ||
fontSize: 22, fontWeight: FontWeight.bold), | ||
), | ||
const SizedBox(height: 20), | ||
Text( | ||
_questions[_currentQuestionIndex].question, | ||
style: const TextStyle(fontSize: 18), | ||
), | ||
const SizedBox(height: 20), | ||
Column( | ||
children: List.generate( | ||
_questions[_currentQuestionIndex].options.length, | ||
(index) => GestureDetector( | ||
onTap: () => _answerQuestion(index), | ||
child: Container( | ||
margin: const EdgeInsets.symmetric(vertical: 4.0), | ||
padding: const EdgeInsets.all(8.0), | ||
decoration: BoxDecoration( | ||
border: Border.all(color: Colors.black, width: 1.0), | ||
borderRadius: BorderRadius.circular(8.0), | ||
color: Colors.blueAccent.withOpacity(0.2), | ||
), | ||
child: Row( | ||
children: [ | ||
SizedBox( | ||
width: 30, | ||
height: 30, | ||
child: | ||
SvgPicture.asset('assets/explore/dot.svg'), | ||
), | ||
const SizedBox(width: 12.0), | ||
Expanded( | ||
child: Text( | ||
_questions[_currentQuestionIndex] | ||
.options[index], | ||
style: const TextStyle(fontSize: 16), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
), | ||
), | ||
const SizedBox(height: 20), | ||
if (_feedbackMessage.isNotEmpty) | ||
Container( | ||
padding: const EdgeInsets.all(12.0), | ||
decoration: BoxDecoration( | ||
color: _feedbackColor.withOpacity(0.2), | ||
borderRadius: BorderRadius.circular(8.0), | ||
border: Border.all(color: _feedbackColor, width: 1.0), | ||
), | ||
child: Text( | ||
_feedbackMessage, | ||
style: TextStyle( | ||
fontSize: 16, | ||
fontWeight: FontWeight.bold, | ||
color: _feedbackColor, | ||
), | ||
), | ||
), | ||
const SizedBox(height: 20), | ||
if (_answered && !_quizCompleted) | ||
ElevatedButton( | ||
onPressed: _nextQuestion, | ||
child: const Text('Next Question'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does Multidex Enabling Must @Prashant-2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i recently upgraded my flutter, since then i had to add this in build