From bbb7914fc5360bd46a6fd0df4710bc53456e9af6 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Thu, 1 Jan 2026 16:39:57 +0100 Subject: [PATCH] Updated sorting logic & added comments --- .../widgets/player_selection.dart | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/presentation/widgets/player_selection.dart b/lib/presentation/widgets/player_selection.dart index eb70ae0..5f77e1f 100644 --- a/lib/presentation/widgets/player_selection.dart +++ b/lib/presentation/widgets/player_selection.dart @@ -149,18 +149,24 @@ class _PlayerSelectionState extends State { onIconTap: () { setState(() { // Removes the player from the selection and notifies the parent. + selectedPlayers.remove(player); + widget.onChanged([...selectedPlayers]); + + // Get the current search query final currentSearch = _searchBarController .text .toLowerCase(); - selectedPlayers.remove(player); - widget.onChanged([...selectedPlayers]); + // If the player matches the current search query (or search is empty), - // they are added back to the suggestions and the list is re-sorted. + // they are added back to the `suggestedPlayers` and the list is re-sorted. if (currentSearch.isEmpty || player.name.toLowerCase().contains( currentSearch, )) { suggestedPlayers.add(player); + suggestedPlayers.sort( + (a, b) => a.name.compareTo(b.name), + ); } }); }, @@ -200,11 +206,15 @@ class _PlayerSelectionState extends State { text: suggestedPlayers[index].name, onPressed: () { setState(() { + // If the player is not already selected if (!selectedPlayers.contains( suggestedPlayers[index], )) { - selectedPlayers.add(suggestedPlayers[index]); + // Add to player to the front of the selectedPlayers + selectedPlayers.insert(0, suggestedPlayers[index]); + // Notify the parent widget of the change widget.onChanged([...selectedPlayers]); + // Remove the player from the suggestedPlayers suggestedPlayers.remove(suggestedPlayers[index]); } }); @@ -221,7 +231,7 @@ class _PlayerSelectionState extends State { } /// Adds a new player to the database from the search bar input. - /// Shows a snackbar indicating success or failure. + /// Shows a snackbar indicating success xfor failure. /// [context] - BuildContext to show the snackbar. void addNewPlayerFromSearch({required BuildContext context}) async { String playerName = _searchBarController.text.trim();