side bar widget uses context

This commit is contained in:
hannathkadher
2024-11-28 19:52:20 +04:00
parent d02d680520
commit b75f8a3440

View File

@ -42,7 +42,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_selectedId = widget.selectedSpaceUuid; // Initialize with the passed selected space UUID _selectedId = widget
.selectedSpaceUuid; // Initialize with the passed selected space UUID
} }
@override @override
@ -83,8 +84,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
return widget.communities.where((community) { return widget.communities.where((community) {
final containsQueryInCommunity = final containsQueryInCommunity =
community.name.toLowerCase().contains(_searchQuery.toLowerCase()); community.name.toLowerCase().contains(_searchQuery.toLowerCase());
final containsQueryInSpaces = final containsQueryInSpaces = community.spaces
community.spaces.any((space) => _containsQuery(space, _searchQuery.toLowerCase())); .any((space) => _containsQuery(space, _searchQuery.toLowerCase()));
return containsQueryInCommunity || containsQueryInSpaces; return containsQueryInCommunity || containsQueryInSpaces;
}).toList(); }).toList();
@ -93,8 +94,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
// Helper function to determine if any space or its children match the search query // Helper function to determine if any space or its children match the search query
bool _containsQuery(SpaceModel space, String query) { bool _containsQuery(SpaceModel space, String query) {
final matchesSpace = space.name.toLowerCase().contains(query); final matchesSpace = space.name.toLowerCase().contains(query);
final matchesChildren = final matchesChildren = space.children.any((child) =>
space.children.any((child) => _containsQuery(child, query)); // Recursive check for children _containsQuery(child, query)); // Recursive check for children
// If the space or any of its children match the query, expand this space // If the space or any of its children match the query, expand this space
if (matchesSpace || matchesChildren) { if (matchesSpace || matchesChildren) {
@ -128,7 +129,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
width: 300, width: 300,
decoration: subSectionContainerDecoration, decoration: subSectionContainerDecoration,
child: Column( child: Column(
mainAxisSize: MainAxisSize.min, // Ensures the Column only takes necessary height mainAxisSize:
MainAxisSize.min, // Ensures the Column only takes necessary height
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// Communities title with the add button // Communities title with the add button
@ -176,7 +178,7 @@ class _SidebarWidgetState extends State<SidebarWidget> {
Expanded( Expanded(
child: ListView( child: ListView(
children: filteredCommunities.map((community) { children: filteredCommunities.map((community) {
return _buildCommunityTile(community); return _buildCommunityTile(context, community);
}).toList(), }).toList(),
), ),
), ),
@ -185,7 +187,7 @@ class _SidebarWidgetState extends State<SidebarWidget> {
); );
} }
Widget _buildCommunityTile(CommunityModel community) { Widget _buildCommunityTile(BuildContext context, CommunityModel community) {
bool hasChildren = community.spaces.isNotEmpty; bool hasChildren = community.spaces.isNotEmpty;
return CommunityTile( return CommunityTile(
@ -199,16 +201,17 @@ class _SidebarWidgetState extends State<SidebarWidget> {
_selectedSpaceUuid = null; // Update the selected community _selectedSpaceUuid = null; // Update the selected community
}); });
if (widget.onCommunitySelected != null) { context.read<SpaceManagementBloc>().add(
widget.onCommunitySelected!(community); SelectCommunityEvent(selectedCommunity: community),
widget.onSpaceSelected!(null); // Pass the entire community );
}
}, },
onExpansionChanged: (String title, bool expanded) { onExpansionChanged: (String title, bool expanded) {
_handleExpansionChange(community.uuid, expanded); _handleExpansionChange(community.uuid, expanded);
}, },
children: hasChildren children: hasChildren
? community.spaces.map((space) => _buildSpaceTile(space, community)).toList() ? community.spaces
.map((space) => _buildSpaceTile(space, community))
.toList()
: null, // Render spaces within the community : null, // Render spaces within the community
); );
} }
@ -230,17 +233,15 @@ class _SidebarWidgetState extends State<SidebarWidget> {
_selectedSpaceUuid = space.uuid; _selectedSpaceUuid = space.uuid;
}); });
if (widget.onSpaceSelected != null) { context.read<SpaceManagementBloc>().add(
widget.onCommunitySelected!(community); SelectSpaceEvent(
widget.onSpaceSelected!(space); selectedCommunity: community, selectedSpace: space),
} );
if (widget.onSelectedSpaceChanged != null) {
widget.onSelectedSpaceChanged!(space.uuid);
}
}, },
children: space.children.isNotEmpty children: space.children.isNotEmpty
? space.children.map((childSpace) => _buildSpaceTile(childSpace, community)).toList() ? space.children
.map((childSpace) => _buildSpaceTile(childSpace, community))
.toList()
: [], // Recursively render child spaces if available : [], // Recursively render child spaces if available
); );
} }