-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults_screen.dart
76 lines (69 loc) · 2.13 KB
/
results_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import 'package:flutter/material.dart';
import 'package:quiz_app/data/questions.dart';
import 'package:quiz_app/questions_summary/questions_summary.dart';
class ResultsScreen extends StatelessWidget {
const ResultsScreen({
super.key,
required this.chosenAnswers,
required this.onRestart
});
final void Function() onRestart;
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(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, // Using as much width as available
child: Container(
margin: const EdgeInsetsDirectional.all(40),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"You've answered $numCorrectQuestions out of $numTotalQuestions questions correctly!",
style: TextStyle(
color: const Color.fromARGB(255, 141, 105, 163),
fontSize: 20,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(
height: 30,
),
QuestionsSummary(summaryData),
const SizedBox(
height: 30,
),
TextButton.icon(
onPressed: onRestart,
style: TextButton.styleFrom(
foregroundColor: Colors.white,
),
icon: const Icon(Icons.refresh),
label: const Text('Restart Quiz!'),
)
],
),
),
);
}
}