Skip to content

Commit

Permalink
quiz ui improve
Browse files Browse the repository at this point in the history
  • Loading branch information
uumair327 committed May 13, 2024
1 parent 9fffe15 commit b3778d3
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
73 changes: 73 additions & 0 deletions lib/src/common_widgets/quiz_question_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:flutter/material.dart';

class QuizQuestionWidget extends StatelessWidget {
final int questionIndex;
final Map<String, dynamic> question;
final Function(int) onPressed;

const QuizQuestionWidget({
Key? key,
required this.questionIndex,
required this.question,
required this.onPressed,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Expanded(
child: Container(
color: Colors.green, // Background color
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Question $questionIndex',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white, // Text color
),
),
const SizedBox(height: 16),
Text(
question['question'],
style: const TextStyle(
fontSize: 20,
color: Colors.white, // Text color
),
),
const SizedBox(height: 16),
...List.generate(
question['options'].length,
(index) => SingleChildScrollView(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 8.0),
child: ElevatedButton(
onPressed: () => onPressed(index),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
side: const BorderSide(
color: Colors.white), // White border
),
padding: const EdgeInsets.symmetric(
vertical: 16.0,
horizontal: 24.0,
),
backgroundColor: Colors.green, // Green background color
),
child: Text(
question['options'][index],
style: const TextStyle(color: Colors.white), // Text color
),
),
),
),
),
],
),
),
);
}
}
25 changes: 25 additions & 0 deletions lib/src/features/quiz/controllers/quiz_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';

class QuizController {
static Future<void> showQuizCompletedDialog(BuildContext context, int correctAnswers, int totalQuestions) async {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Quiz Completed'),
content: Text(
'You have completed the quiz!\n\nYour score: $correctAnswers/$totalQuestions',
),
actions: [
TextButton(
onPressed: () {
Navigator.popUntil(context, ModalRoute.withName('/'));
},
child: const Text('OK'),
),
],
);
},
);
}
}
67 changes: 67 additions & 0 deletions lib/src/features/quiz/screens/quiz_questions_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'package:flutter/material.dart';
import 'package:guardiancare/src/common_widgets/quiz_question_widget.dart';
import 'package:guardiancare/src/features/quiz/controllers/quiz_controller.dart';

class QuizQuestionsPage extends StatefulWidget {
final List<Map<String, dynamic>> questions;

QuizQuestionsPage({required this.questions});

@override
_QuizQuestionsPageState createState() => _QuizQuestionsPageState();
}

class _QuizQuestionsPageState extends State<QuizQuestionsPage> {
int currentQuestionIndex = 0; // Index of the current question
int correctAnswers = 0; // Number of correct answers

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Quiz Questions'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
QuizQuestionWidget(
questionIndex: currentQuestionIndex + 1,
question: widget.questions[currentQuestionIndex],
onPressed: (int selectedOptionIndex) {
// Check if the selected option is correct
if (selectedOptionIndex ==
widget.questions[currentQuestionIndex]
['correctAnswerIndex']) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Correct!'),
));
setState(() {
correctAnswers++;
});
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Incorrect!'),
));
}
// Move to the next question after a short delay
Future.delayed(const Duration(seconds: 2), () {
setState(() {
if (currentQuestionIndex < widget.questions.length - 1) {
currentQuestionIndex++;
} else {
// Show quiz completed dialog
QuizController.showQuizCompletedDialog(
context, correctAnswers, widget.questions.length);
}
});
});
},
),
],
),
),
);
}
}
2 changes: 1 addition & 1 deletion lib/src/screens/quizPage.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:guardiancare/src/screens/quizQuestionsPage.dart';
import 'package:guardiancare/src/features/quiz/screens/quiz_questions_page.dart';

class QuizPage extends StatelessWidget {
@override
Expand Down

0 comments on commit b3778d3

Please sign in to comment.