Skip to content
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 9 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (flutterVersionName == null) {
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
Expand Down Expand Up @@ -54,30 +54,33 @@ android {
applicationId "vdrs.sappu.lafk.learn"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 21
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
Copy link
Collaborator

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

Copy link
Contributor Author

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

}

signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}

}

flutter {
source '../..'
}

dependencies {}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
9 changes: 9 additions & 0 deletions assets/explore/dot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions assets/explore/notebook.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions devtools_options.yaml
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:
181 changes: 181 additions & 0 deletions lib/explore/quiz.dart
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'),
),
],
),
),
);
}
}
5 changes: 2 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:learn/pages/modules/colours.dart';
import 'package:learn/widgets/navbar/navbar.dart';

import 'cubit/index_cubit.dart';
import 'explore/quiz.dart';
import 'pages/home.dart';

DateTime? currentBackPressTime;
Expand Down Expand Up @@ -68,6 +69,7 @@ class MyApp extends StatelessWidget {
AllRoutes.flowerRoute: (context) => const FlowerPage(),
AllRoutes.exploreRoute: (context) => const ExplorePage(),
AllRoutes.favoriteRoute: (context) => const FavoritePage(),
AllRoutes.quizRoute: (context) => QuizPage(),
},
);
},
Expand All @@ -76,6 +78,3 @@ class MyApp extends StatelessWidget {
);
}
}



Loading