-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
166 additions
and
1 deletion.
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
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 | ||
), | ||
), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
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,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'), | ||
), | ||
], | ||
); | ||
}, | ||
); | ||
} | ||
} |
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,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); | ||
} | ||
}); | ||
}); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
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