From 0c1ae39433e4f2f170b6a02065db209211cd03b5 Mon Sep 17 00:00:00 2001 From: Connor Frank Date: Mon, 1 Apr 2024 19:04:41 -0400 Subject: Tapping Total Par Shows Ranking --- lib/scorekeeper.dart | 61 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/lib/scorekeeper.dart b/lib/scorekeeper.dart index 57cd1ed..0c67f6e 100644 --- a/lib/scorekeeper.dart +++ b/lib/scorekeeper.dart @@ -326,6 +326,52 @@ class _ScoreKeeperState extends State { ); } + void _showPlayerRankings(BuildContext context) { + // Calculate total scores for each player + Map totalScores = {}; + for (var name in playerNames) { + int totalScore = 0; + scores.forEach((hole, playerScores) { + int playerIndex = playerNames.indexOf(name); + if (playerScores.length > playerIndex) { + totalScore += playerScores[playerIndex]; + } + }); + totalScores[name] = totalScore; + } + + // Remove players with a total score of 0 + totalScores.removeWhere((name, score) => score == 0); + + // Sort players by total score + var sortedScores = totalScores.entries.toList() + ..sort((a, b) => a.value.compareTo(b.value)); + + // Create a list of player names and scores for display, sorted by score + List scoreWidgets = sortedScores.map((entry) { + return Text('${entry.key}: ${entry.value}'); + }).toList(); + + // Show the rankings in a dialog + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Player Rankings'), + content: SingleChildScrollView( + child: ListBody(children: scoreWidgets), + ), + actions: [ + TextButton( + child: const Text('OK'), + onPressed: () => Navigator.of(context).pop(), + ), + ], + ); + }, + ); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -395,13 +441,18 @@ class _ScoreKeeperState extends State { mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ // Total Par Display - Padding( - padding: const EdgeInsets.symmetric(horizontal: 8.0), - child: Text( - 'Total Par: ${pars.values.fold(0, (prev, par) => prev + par)}', - style: const TextStyle(fontWeight: FontWeight.bold), + GestureDetector( + onTap: () => _showPlayerRankings(context), + child: Container( + padding: const EdgeInsets.all(10), + color: Colors.blueGrey[100], + child: Text( + 'Total Par: ${pars.values.fold(0, (prev, par) => prev + par)}', + style: const TextStyle(fontSize: 16), + ), ), ), + // Divider between Total Par and Players' Scores const VerticalDivider(color: Colors.black), // Players' Scores -- cgit v1.2.3