import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:syncrow_web/common/search_bar.dart'; import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/community_tile.dart'; import 'package:syncrow_web/pages/spaces_management/view/dialogs/create_community_dialog.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/space_tile_widget.dart'; import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/style.dart'; class SidebarWidget extends StatefulWidget { final Function(String)? onCommunitySelected; final List communities; SidebarWidget({this.onCommunitySelected, required this.communities}); @override _SidebarWidgetState createState() => _SidebarWidgetState(); } class _SidebarWidgetState extends State { String _searchQuery = ''; // Track search query Map _expandedTiles = {}; // Track expanded state for each UUID @override void initState() { super.initState(); } void _showCreateCommunityDialog() { showDialog( context: context, builder: (context) => CreateCommunityDialog( onCreateCommunity: (String communityName) { setState(() { // You can update the community list here when a new community is added }); }, ), ); } // Function to filter communities based on the search query List _filterCommunities() { if (_searchQuery.isEmpty) { return widget.communities; } // Filter communities and their spaces based on the search query return widget.communities.where((community) { final containsQueryInCommunity = community.name.toLowerCase().contains(_searchQuery.toLowerCase()); final containsQueryInSpaces = community.spaces .any((space) => _containsQuery(space, _searchQuery.toLowerCase())); 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)) { return true; } for (var child in space.children) { if (_containsQuery(child, query)) { return true; } } return false; } @override Widget build(BuildContext context) { final filteredCommunities = _filterCommunities(); return Container( width: 300, decoration: subSectionContainerDecoration, child: Column( mainAxisSize: MainAxisSize.min, // Ensures the Column only takes necessary height crossAxisAlignment: CrossAxisAlignment.start, children: [ // Communities title with the add button Container( decoration: subSectionContainerDecoration, padding: const EdgeInsets.all(16.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'Communities', style: Theme.of(context).textTheme.titleMedium, ), GestureDetector( onTap: _showCreateCommunityDialog, child: Container( width: 30, height: 30, decoration: const BoxDecoration( color: ColorsManager.whiteColors, shape: BoxShape.circle, ), child: Center( child: SvgPicture.asset( Assets.roundedAddIcon, width: 24, height: 24, ), ), ), ), ], ), ), // Search bar CustomSearchBar( onSearchChanged: (query) { setState(() { _searchQuery = query; }); }, ), const SizedBox(height: 16), // Community list Expanded( child: ListView( children: filteredCommunities.map((community) { return _buildCommunityTile(community); }).toList(), ), ), ], ), ); } Widget _buildCommunityTile(CommunityModel community) { bool hasChildren = community.spaces.isNotEmpty; debugPrint( 'Building CommunityTile for ${community.name}, hasChildren: $hasChildren'); return CommunityTile( title: community.name, initiallyExpanded: false, onExpansionChanged: (String title, bool expanded) { debugPrint( 'CommunityTile onExpansionChanged called for $title, expanded: $expanded'); _handleExpansionChange(community.uuid, expanded); }, children: hasChildren ? community.spaces.map((space) => _buildSpaceTile(space)).toList() : null, // Render spaces within the community ); } Widget _buildSpaceTile(SpaceModel space) { debugPrint( 'Building SpaceTile for ${space.name}, hasChildren: ${space.children.isNotEmpty}'); return SpaceTile( title: space.name, initiallyExpanded: false, onExpansionChanged: (bool expanded) { debugPrint( 'SpaceTile onExpansionChanged called for ${space.name}, expanded: $expanded'); _handleExpansionChange(space.uuid, expanded); }, children: space.children.isNotEmpty ? space.children .map((childSpace) => _buildSpaceTile(childSpace)) .toList() : [], // Recursively render child spaces if available ); } void _handleExpansionChange(String uuid, bool expanded) {} }