From b4ba4f8d74bcb842bda144f5b90da00b407f43db Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Sun, 7 Dec 2025 19:12:43 +0100 Subject: [PATCH] Implemented search bar in choose_group_view --- .../create_game/choose_group_view.dart | 56 ++++++++++++++----- .../create_game/create_game_view.dart | 8 +-- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lib/presentation/views/main_menu/create_game/choose_group_view.dart b/lib/presentation/views/main_menu/create_game/choose_group_view.dart index f805d6e..e27adf3 100644 --- a/lib/presentation/views/main_menu/create_game/choose_group_view.dart +++ b/lib/presentation/views/main_menu/create_game/choose_group_view.dart @@ -6,12 +6,12 @@ import 'package:game_tracker/presentation/widgets/tiles/group_tile.dart'; class ChooseGroupView extends StatefulWidget { final List groups; - final int initialGroupIndex; + final String initialGroupId; const ChooseGroupView({ super.key, required this.groups, - required this.initialGroupIndex, + required this.initialGroupId, }); @override @@ -19,13 +19,15 @@ class ChooseGroupView extends StatefulWidget { } class _ChooseGroupViewState extends State { - late int selectedGroupIndex; + late String selectedGroupId; final TextEditingController controller = TextEditingController(); final String hintText = 'Group Name'; + late final List filteredGroups; @override void initState() { - selectedGroupIndex = widget.initialGroupIndex; + selectedGroupId = widget.initialGroupId; + filteredGroups = [...widget.groups]; super.initState(); } @@ -40,9 +42,11 @@ class _ChooseGroupViewState extends State { icon: const Icon(Icons.arrow_back_ios), onPressed: () { Navigator.of(context).pop( - selectedGroupIndex == -1 + selectedGroupId == '' ? null - : widget.groups[selectedGroupIndex], + : widget.groups.firstWhere( + (group) => group.id == selectedGroupId, + ), ); }, ), @@ -56,26 +60,34 @@ class _ChooseGroupViewState extends State { children: [ Padding( padding: const EdgeInsets.symmetric(horizontal: 10), - child: CustomSearchBar(controller: controller, hintText: hintText), + child: CustomSearchBar( + controller: controller, + hintText: hintText, + onChanged: (value) { + setState(() { + filterGroups(value); + }); + }, + ), ), Expanded( child: ListView.builder( padding: const EdgeInsets.only(bottom: 85), - itemCount: widget.groups.length, + itemCount: filteredGroups.length, itemBuilder: (BuildContext context, int index) { return GestureDetector( onTap: () { setState(() { - if (selectedGroupIndex == index) { - selectedGroupIndex = -1; + if (selectedGroupId != filteredGroups[index].id) { + selectedGroupId = filteredGroups[index].id; } else { - selectedGroupIndex = index; + selectedGroupId = ''; } }); }, child: GroupTile( - group: widget.groups[index], - isHighlighted: selectedGroupIndex == index, + group: filteredGroups[index], + isHighlighted: selectedGroupId == filteredGroups[index].id, ), ); }, @@ -85,4 +97,22 @@ class _ChooseGroupViewState extends State { ), ); } + + /// Filters the groups based on the search query. + /// TODO: Maybe implement also targetting player names? + void filterGroups(String query) { + setState(() { + if (query.isEmpty) { + filteredGroups.clear(); + filteredGroups.addAll(widget.groups); + } else { + filteredGroups.clear(); + filteredGroups.addAll( + widget.groups.where( + (group) => group.name.toLowerCase().contains(query.toLowerCase()), + ), + ); + } + }); + } } diff --git a/lib/presentation/views/main_menu/create_game/create_game_view.dart b/lib/presentation/views/main_menu/create_game/create_game_view.dart index 4bf05e7..fdae05c 100644 --- a/lib/presentation/views/main_menu/create_game/create_game_view.dart +++ b/lib/presentation/views/main_menu/create_game/create_game_view.dart @@ -47,7 +47,7 @@ class _CreateGameViewState extends State { /// The index of the currently selected group in [groupsList] to mark it in /// the [ChooseGroupView] - int selectedGroupIndex = -1; + String selectedGroupId = ''; /// The currently selected ruleset Ruleset? selectedRuleset; @@ -189,13 +189,11 @@ class _CreateGameViewState extends State { MaterialPageRoute( builder: (context) => ChooseGroupView( groups: groupsList, - initialGroupIndex: selectedGroupIndex, + initialGroupId: selectedGroupId, ), ), ); - selectedGroupIndex = groupsList.indexWhere( - (g) => g.id == selectedGroup?.id, - ); + selectedGroupId = selectedGroup?.id ?? ''; setState(() {}); }, ),