filtering

This commit is contained in:
hannathkadher
2024-10-08 23:51:01 +04:00
parent 33712b7690
commit 3a94ebedda
2 changed files with 38 additions and 9 deletions

View File

@ -95,9 +95,7 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
child: Text(
_capitalizeFirstLetter(widget.title),
style: TextStyle(
color: _isExpanded
? ColorsManager.blackColor
: ColorsManager.lightGrayColor,
color:ColorsManager.lightGrayColor,
fontWeight: FontWeight.w400,
),
),

View File

@ -22,7 +22,8 @@ class SidebarWidget extends StatefulWidget {
class _SidebarWidgetState extends State<SidebarWidget> {
String _searchQuery = ''; // Track search query
Map<String, bool> _expandedTiles = {}; // Track expanded state for each UUID
String? _selectedCommunityUuid;
String? _selectedSpaceUuid;
@override
void initState() {
@ -45,29 +46,55 @@ class _SidebarWidgetState extends State<SidebarWidget> {
// Function to filter communities based on the search query
List<CommunityModel> _filterCommunities() {
if (_searchQuery.isEmpty) {
// Reset the selected community and space UUIDs if there's no query
_selectedCommunityUuid = null;
_selectedSpaceUuid = null;
return widget.communities;
}
// Filter communities and their spaces based on the search query
// Filter communities and expand only those that match the query
return widget.communities.where((community) {
final containsQueryInCommunity =
community.name.toLowerCase().contains(_searchQuery.toLowerCase());
final containsQueryInSpaces = community.spaces
.any((space) => _containsQuery(space, _searchQuery.toLowerCase()));
if (containsQueryInCommunity || containsQueryInSpaces) {
_selectedCommunityUuid =
community.uuid; // Set the community to expanded
}
return containsQueryInCommunity || containsQueryInSpaces;
}).toList();
}
// Helper function to determine if any space or its children match the search query
bool _containsQuery(SpaceModel space, String query) {
if (space.name.toLowerCase().contains(query)) {
final matchesSpace = space.name.toLowerCase().contains(query);
final matchesChildren = space.children.any((child) =>
_containsQuery(child, query)); // Recursive check for children
// If the space or any of its children match the query, expand this space
if (matchesSpace || matchesChildren) {
_selectedSpaceUuid = space.uuid;
}
return matchesSpace || matchesChildren;
}
bool _isSpaceOrChildSelected(SpaceModel space) {
// Return true if the current space or any of its child spaces is selected
if (_selectedSpaceUuid == space.uuid) {
return true;
}
// Recursively check if any child spaces match the query
for (var child in space.children) {
if (_containsQuery(child, query)) {
if (_isSpaceOrChildSelected(child)) {
return true;
}
}
return false;
}
@ -139,12 +166,13 @@ class _SidebarWidgetState extends State<SidebarWidget> {
Widget _buildCommunityTile(CommunityModel community) {
bool hasChildren = community.spaces.isNotEmpty;
bool isSelectedCommunity = _selectedCommunityUuid == community.uuid;
debugPrint(
'Building CommunityTile for ${community.name}, hasChildren: $hasChildren');
return CommunityTile(
title: community.name,
initiallyExpanded: false,
initiallyExpanded: isSelectedCommunity,
onExpansionChanged: (String title, bool expanded) {
debugPrint(
'CommunityTile onExpansionChanged called for $title, expanded: $expanded');
@ -157,11 +185,14 @@ class _SidebarWidgetState extends State<SidebarWidget> {
}
Widget _buildSpaceTile(SpaceModel space) {
bool isSelectedSpace =
_isSpaceOrChildSelected(space); // Check if space should be expanded
debugPrint(
'Building SpaceTile for ${space.name}, hasChildren: ${space.children.isNotEmpty}');
return SpaceTile(
title: space.name,
initiallyExpanded: false,
initiallyExpanded: isSelectedSpace,
onExpansionChanged: (bool expanded) {
debugPrint(
'SpaceTile onExpansionChanged called for ${space.name}, expanded: $expanded');