To create a total summary page for your quizzes, you need to:
- Fetch the total number of quizzes from your Blogger API.
- Calculate the total number of questions, total time, and total marks from all quizzes.
- Display a summary page with this information.
1. Modify BloggerService to Calculate Totals
In blogger_service.dart, update the fetch function:
import 'dart:convert';
import 'package:http/http.dart' as http;
class BloggerService {
static const String bloggerId = "YOUR_BLOGGER_ID";
static const String apiKey = "YOUR_API_KEY";
static const String apiUrl = "https://www.googleapis.com/blogger/v3/blogs/$bloggerId/posts?key=$apiKey";
static Future<Map<String, dynamic>> fetchQuizzesWithTotals() async {
final response = await http.get(Uri.parse(apiUrl));
if (response.statusCode == 200) {
final data = json.decode(response.body);
List<Map<String, dynamic>> quizzes = [];
int totalQuizzes = 0;
int totalQuestions = 0;
int totalTime = 0;
int totalMarks = 0;
for (var item in data["items"]) {
String content = item["content"];
RegExp regex = RegExp(r"Total Questions: (\d+)\nTime: (\d+) mins\nMarks: (\d+)\nPaid: (\w+)");
Match? match = regex.firstMatch(content);
if (match != null) {
int questions = int.parse(match.group(1)!);
int time = int.parse(match.group(2)!);
int marks = int.parse(match.group(3)!);
quizzes.add({
"title": item["title"],
"totalQuestions": questions,
"time": time,
"marks": marks,
"paid": match.group(4)! == "Yes",
"url": item["url"]
});
totalQuizzes++;
totalQuestions += questions;
totalTime += time;
totalMarks += marks;
}
}
return {
"quizzes": quizzes,
"totalQuizzes": totalQuizzes,
"totalQuestions": totalQuestions,
"totalTime": totalTime,
"totalMarks": totalMarks,
};
} else {
throw Exception("Failed to load quizzes");
}
}
}
2. Create a Summary Page
Now, create quiz_summary_screen.dart to display total data:
import 'package:flutter/material.dart';
import 'blogger_service.dart';
class QuizSummaryScreen extends StatefulWidget {
const QuizSummaryScreen({super.key});
@override
_QuizSummaryScreenState createState() => _QuizSummaryScreenState();
}
class _QuizSummaryScreenState extends State<QuizSummaryScreen> {
late Future<Map<String, dynamic>> quizzesFuture;
@override
void initState() {
super.initState();
quizzesFuture = BloggerService.fetchQuizzesWithTotals();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Quiz Summary")),
body: FutureBuilder<Map<String, dynamic>>(
future: quizzesFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text("Error: ${snapshot.error}"));
} else if (!snapshot.hasData) {
return const Center(child: Text("No data available"));
}
final data = snapshot.data!;
final quizzes = data["quizzes"] as List<Map<String, dynamic>>;
return Column(
children: [
// Total Summary Card
Card(
margin: const EdgeInsets.all(10),
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text("Overall Quiz Summary",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
const SizedBox(height: 10),
Text("Total Quizzes: ${data["totalQuizzes"]}"),
Text("Total Questions: ${data["totalQuestions"]}"),
Text("Total Time: ${data["totalTime"]} mins"),
Text("Total Marks: ${data["totalMarks"]}"),
],
),
),
),
// List of Quizzes
Expanded(
child: ListView.builder(
itemCount: quizzes.length,
itemBuilder: (context, index) {
var quiz = quizzes[index];
return Card(
margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
elevation: 3,
child: ListTile(
title: Text(quiz["title"], style: const TextStyle(fontWeight: FontWeight.bold)),
subtitle: Text(
"Questions: ${quiz["totalQuestions"]} | Time: ${quiz["time"]} mins | Marks: ${quiz["marks"]}",
style: const TextStyle(fontSize: 14),
),
trailing: ElevatedButton(
onPressed: () {
print("Open quiz: ${quiz["url"]}");
},
style: ElevatedButton.styleFrom(
backgroundColor: quiz["paid"] ? Colors.green : Colors.red,
),
child: Text(quiz["paid"] ? "Paid" : "Free"),
),
),
);
},
),
),
],
);
},
),
);
}
}
3. Update main.dart to Open Summary Page
Modify main.dart:
import 'package:flutter/material.dart';
import 'quiz_summary_screen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: const QuizSummaryScreen(),
);
}
}
4. Run the App
Now, run:
flutter run
Your total summary page will now:
✅ Show total quizzes, total questions, total time, and total marks
✅ Display a list of quizzes with title, number of questions, time, marks
✅ Highlight "Free" in red and "Paid" in green
✅ Allow users to open quiz links
🚀 Now your app dynamically summarizes quizzes from your Blogger website! Let me know if you need more help. 😊
