fix: sorted games

This commit is contained in:
2026-05-21 19:54:46 +02:00
parent a497ae872b
commit a803dc36d7

View File

@@ -51,6 +51,9 @@ class _ChooseGameViewState extends State<ChooseGameView> {
/// Games filtered according to the current search query
late List<Game> filteredGames;
List<Game> get games =>
widget.games..sort((a, b) => a.name.compareTo(b.name));
@override
void initState() {
db = Provider.of<AppDatabase>(context, listen: false);
@@ -59,7 +62,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
selectedGameId = widget.initialGameId;
// Start with all games visible
filteredGames = List<Game>.from(widget.games);
filteredGames = List<Game>.from(games);
super.initState();
}
@@ -77,9 +80,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
Navigator.of(context).pop(
selectedGameId == ''
? null
: widget.games.firstWhere(
(game) => game.id == selectedGameId,
),
: games.firstWhere((game) => game.id == selectedGameId),
);
},
),
@@ -99,7 +100,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
);
if (result != null && result.game != null) {
setState(() {
widget.games.insert(0, result.game);
games.insert(0, result.game);
});
_refreshFromSource();
}
@@ -139,7 +140,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
child: Visibility(
visible: filteredGames.isNotEmpty,
replacement: Visibility(
visible: widget.games.isNotEmpty,
visible: games.isNotEmpty,
replacement: TopCenteredMessage(
icon: Icons.info,
title: loc.info,
@@ -190,7 +191,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
);
if (result != null && result.game != null) {
// Find the index in the original list to mutate
final originalIndex = widget.games.indexWhere(
final originalIndex = games.indexWhere(
(g) => g.id == game.id,
);
if (originalIndex == -1) {
@@ -202,12 +203,12 @@ class _ChooseGameViewState extends State<ChooseGameView> {
if (selectedGameId == game.id) {
selectedGameId = '';
}
widget.games.removeAt(originalIndex);
games.removeAt(originalIndex);
widget.onGamesUpdated?.call();
});
} else {
setState(() {
widget.games[originalIndex] = result.game;
games[originalIndex] = result.game;
});
}
_refreshFromSource();
@@ -229,13 +230,13 @@ class _ChooseGameViewState extends State<ChooseGameView> {
final q = query.toLowerCase().trim();
if (q.isEmpty) {
setState(() {
filteredGames = List<Game>.from(widget.games);
filteredGames = List<Game>.from(games);
});
return;
}
setState(() {
filteredGames = widget.games.where((game) {
filteredGames = games.where((game) {
final name = game.name.toLowerCase();
final description = game.description.toLowerCase();
return name.contains(q) || description.contains(q);