Implemented search bar in choose_group_view

This commit is contained in:
2025-12-07 19:12:43 +01:00
parent f60c11fc08
commit b4ba4f8d74
2 changed files with 46 additions and 18 deletions

View File

@@ -6,12 +6,12 @@ import 'package:game_tracker/presentation/widgets/tiles/group_tile.dart';
class ChooseGroupView extends StatefulWidget { class ChooseGroupView extends StatefulWidget {
final List<Group> groups; final List<Group> groups;
final int initialGroupIndex; final String initialGroupId;
const ChooseGroupView({ const ChooseGroupView({
super.key, super.key,
required this.groups, required this.groups,
required this.initialGroupIndex, required this.initialGroupId,
}); });
@override @override
@@ -19,13 +19,15 @@ class ChooseGroupView extends StatefulWidget {
} }
class _ChooseGroupViewState extends State<ChooseGroupView> { class _ChooseGroupViewState extends State<ChooseGroupView> {
late int selectedGroupIndex; late String selectedGroupId;
final TextEditingController controller = TextEditingController(); final TextEditingController controller = TextEditingController();
final String hintText = 'Group Name'; final String hintText = 'Group Name';
late final List<Group> filteredGroups;
@override @override
void initState() { void initState() {
selectedGroupIndex = widget.initialGroupIndex; selectedGroupId = widget.initialGroupId;
filteredGroups = [...widget.groups];
super.initState(); super.initState();
} }
@@ -40,9 +42,11 @@ class _ChooseGroupViewState extends State<ChooseGroupView> {
icon: const Icon(Icons.arrow_back_ios), icon: const Icon(Icons.arrow_back_ios),
onPressed: () { onPressed: () {
Navigator.of(context).pop( Navigator.of(context).pop(
selectedGroupIndex == -1 selectedGroupId == ''
? null ? null
: widget.groups[selectedGroupIndex], : widget.groups.firstWhere(
(group) => group.id == selectedGroupId,
),
); );
}, },
), ),
@@ -56,26 +60,34 @@ class _ChooseGroupViewState extends State<ChooseGroupView> {
children: [ children: [
Padding( Padding(
padding: const EdgeInsets.symmetric(horizontal: 10), padding: const EdgeInsets.symmetric(horizontal: 10),
child: CustomSearchBar(controller: controller, hintText: hintText), child: CustomSearchBar(
controller: controller,
hintText: hintText,
onChanged: (value) {
setState(() {
filterGroups(value);
});
},
),
), ),
Expanded( Expanded(
child: ListView.builder( child: ListView.builder(
padding: const EdgeInsets.only(bottom: 85), padding: const EdgeInsets.only(bottom: 85),
itemCount: widget.groups.length, itemCount: filteredGroups.length,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
setState(() { setState(() {
if (selectedGroupIndex == index) { if (selectedGroupId != filteredGroups[index].id) {
selectedGroupIndex = -1; selectedGroupId = filteredGroups[index].id;
} else { } else {
selectedGroupIndex = index; selectedGroupId = '';
} }
}); });
}, },
child: GroupTile( child: GroupTile(
group: widget.groups[index], group: filteredGroups[index],
isHighlighted: selectedGroupIndex == index, isHighlighted: selectedGroupId == filteredGroups[index].id,
), ),
); );
}, },
@@ -85,4 +97,22 @@ class _ChooseGroupViewState extends State<ChooseGroupView> {
), ),
); );
} }
/// 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()),
),
);
}
});
}
} }

View File

@@ -47,7 +47,7 @@ class _CreateGameViewState extends State<CreateGameView> {
/// The index of the currently selected group in [groupsList] to mark it in /// The index of the currently selected group in [groupsList] to mark it in
/// the [ChooseGroupView] /// the [ChooseGroupView]
int selectedGroupIndex = -1; String selectedGroupId = '';
/// The currently selected ruleset /// The currently selected ruleset
Ruleset? selectedRuleset; Ruleset? selectedRuleset;
@@ -189,13 +189,11 @@ class _CreateGameViewState extends State<CreateGameView> {
MaterialPageRoute( MaterialPageRoute(
builder: (context) => ChooseGroupView( builder: (context) => ChooseGroupView(
groups: groupsList, groups: groupsList,
initialGroupIndex: selectedGroupIndex, initialGroupId: selectedGroupId,
), ),
), ),
); );
selectedGroupIndex = groupsList.indexWhere( selectedGroupId = selectedGroup?.id ?? '';
(g) => g.id == selectedGroup?.id,
);
setState(() {}); setState(() {});
}, },
), ),