added leaderboards (without clickthrough to filtered by author - later)

This commit is contained in:
odweta
2026-04-03 13:47:36 +02:00
parent 8789146b9a
commit c7533d8e4a
3 changed files with 85 additions and 13 deletions
+2 -11
View File
@@ -21,18 +21,9 @@ class AddPage extends StatelessWidget {
padding: const EdgeInsets.all(Constants.edgeInset),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
Strings.createQuote,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
TextField(
autofocus: true,
controller: bodyController,
+39 -1
View File
@@ -1,10 +1,48 @@
import 'package:flutter/material.dart';
import 'package:frontend/provider/quote_provider.dart';
import 'package:frontend/values/constants.dart';
import 'package:provider/provider.dart';
class LeaderboardPage extends StatelessWidget {
const LeaderboardPage({super.key});
@override
Widget build(BuildContext context) {
return const Center(child: Text('top 3!'));
final provider = context.watch<QuoteProvider>();
return Center(
child: Column(
children: [
Expanded(
child: ListView.builder(
padding: EdgeInsets.all(Constants.edgeInset),
itemCount: provider.filteredQuotes.length,
itemBuilder: (context, index) {
return
Card(
color: provider.getLeaderboardEntryBgColor(context, index),
child: ListTile(
contentPadding: EdgeInsets.symmetric(
vertical: 0,
horizontal: Constants.edgeInset,
),
title: Text(provider.sortedAuthorCounts[index].key,
style: TextStyle(
color: provider.getLeaderboardEntryFgColor(context, index)
),
),
trailing: Text(provider.sortedAuthorCounts[index].value.toString(),
style: TextStyle(
fontSize: 16,
color: provider.getLeaderboardEntryFgColor(context, index),
),
),
),
);
},
),
),
],
),
);
}
}
+44 -1
View File
@@ -38,7 +38,7 @@ class QuoteProvider extends ChangeNotifier {
return filtered;
}
Map<String, int> get authorCounts {
Map<String, int> get _authorCounts {
final counts = <String, int>{};
for (var quote in filteredQuotes) {
@@ -52,6 +52,23 @@ class QuoteProvider extends ChangeNotifier {
return counts;
}
List<MapEntry<String, int>> get sortedAuthorCounts {
final counts = <String, int>{};
for (var quote in filteredQuotes) {
counts.update(
quote.author,
(value) => value + 1,
ifAbsent: () => 1,
);
}
final list = counts.entries.toList()
..sort((a, b) => b.value.compareTo(a.value)); // descending
return list;
}
final QuoteRepository repo = QuoteRepository();
int filterBy = 0; // 0 = by author, 1 = by quote
@@ -191,4 +208,30 @@ class QuoteProvider extends ChangeNotifier {
final file = File('${dir.path}/hlasky_$datetime.txt');
await file.writeAsString("${filteredQuotes.join("\n")}\n");
}
Color getLeaderboardEntryBgColor(BuildContext context, int index) {
switch (index) {
case 0:
return Colors.yellow.shade300; // 1st place
case 1:
return Colors.grey.shade300; // 2nd place
case 2:
return Colors.brown.shade300; // 3rd place
default:
return Theme.of(context).cardColor; // default for all other entries
}
}
Color getLeaderboardEntryFgColor(BuildContext context, int index) {
switch (index) {
case 0:
return Colors.black; // 1st place
case 1:
return Colors.black; // 2nd place
case 2:
return Colors.black; // 3rd place
default:
return Colors.white; // default for all other entries
}
}
}