You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:adv_basics/questions_summary.dart';
import 'package:adv_basics/data/questions.dart';
import 'package:google_fonts/google_fonts.dart';
class ResultsScreen extends StatelessWidget {
const ResultsScreen({
super.key,
required this.chosenAnswers,
required this.onRestartQuiz,
});
final List<String> chosenAnswers;
final void Function() onRestartQuiz;
List<Map<String, Object>> get summaryData {
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 numTotalQuestions = summaryData.length;
final numCorrectQuestions = summaryData
.where(
(data) => data['user_answer'] == data['correct_answer'],
)
.length;
return SizedBox(
width: double.infinity,
child: Container(
margin: const EdgeInsets.all(40),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'You answered $numCorrectQuestions out of $numTotalQuestions questions correctly!',
style: GoogleFonts.lato(
color: const Color.fromARGB(255, 201, 153, 251),
fontSize: 24,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(
height: 30,
),
QuestionsSummary(
summaryData,
),
const SizedBox(
height: 30,
),
OutlinedButton.icon(
onPressed: onRestartQuiz,
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white,
),
icon: const Icon(Icons.update),
label: const Text('Restart Quiz'),
)
],
),
),
);
}
}