Step 87
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class QuestionsSummary extends StatelessWidget {
|
||||
const QuestionsSummary(this.summaryData, {super.key});
|
||||
|
||||
final List<Map<String, Object>> summaryData;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SizedBox(
|
||||
height: 300,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: summaryData.map(
|
||||
(data) {
|
||||
return Row(
|
||||
children: [
|
||||
Text(((data['question_index'] as int) + 1).toString()),
|
||||
Expanded(
|
||||
child: Column(
|
||||
children: [
|
||||
Text(data['question'] as String),
|
||||
SizedBox(
|
||||
height: 5,
|
||||
),
|
||||
Text(data['user_answer'] as String),
|
||||
Text(data['correct_answer'] as String),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
).toList(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -28,7 +28,6 @@ class _QuizState extends State<Quiz> {
|
||||
|
||||
if (selectedAnswers.length == questions.length) {
|
||||
setState(() {
|
||||
selectedAnswers = [];
|
||||
activeScreen = 'results-screen';
|
||||
});
|
||||
}
|
||||
@@ -45,7 +44,7 @@ class _QuizState extends State<Quiz> {
|
||||
}
|
||||
|
||||
if (activeScreen == 'results-screen') {
|
||||
screenWidget = const ResultsScreen();
|
||||
screenWidget = ResultsScreen(chosenAnswers: selectedAnswers);
|
||||
}
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
|
||||
+34
-3
@@ -1,10 +1,40 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:adv_basics/data/questions.dart';
|
||||
import 'package:adv_basics/questions_summary.dart';
|
||||
|
||||
class ResultsScreen extends StatelessWidget {
|
||||
const ResultsScreen({super.key});
|
||||
const ResultsScreen({
|
||||
super.key,
|
||||
required this.chosenAnswers,
|
||||
});
|
||||
|
||||
final List<String> chosenAnswers;
|
||||
|
||||
List<Map<String, Object>> getSummaryData() {
|
||||
final List<Map<String, Object>> summary = [];
|
||||
|
||||
for (var i = 0; i < chosenAnswers.length; i++) {
|
||||
summary.add(
|
||||
{
|
||||
'question_index': i,
|
||||
'question': questions[i].text,
|
||||
'correct_answer': questions[i].answers[0],
|
||||
'user_answer': chosenAnswers[i],
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final summaryData = getSummaryData();
|
||||
final numTotalQuestions = questions.length;
|
||||
final numCorrectQuestions = summaryData.where((data) {
|
||||
return data['user_answer'] == data['correct_answer'];
|
||||
}).length;
|
||||
|
||||
return SizedBox(
|
||||
width: double.infinity,
|
||||
child: Container(
|
||||
@@ -12,11 +42,12 @@ class ResultsScreen extends StatelessWidget {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text('You answered X out of Y questions correctly!'),
|
||||
Text(
|
||||
'You answered $numCorrectQuestions out of $numTotalQuestions questions correctly!'),
|
||||
const SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
const Text('List of answers and questions...'),
|
||||
QuestionsSummary(summaryData),
|
||||
const SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user