import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:syncrow_web/common/custom_expansion_tile.dart'; import 'package:syncrow_web/common/search_bar.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/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 Map> communitySpaces; SidebarWidget({this.onCommunitySelected, required this.communitySpaces}); @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(); _logCommunitySpaces(); // Log the structure on initialization } // Function to log the communitySpaces data structure for debugging void _logCommunitySpaces() { debugPrint('Logging communitySpaces structure:'); widget.communitySpaces.forEach((communityName, spaces) { debugPrint('Community: $communityName'); _logSpaces(spaces, 1); }); } // Helper function to log the spaces and their children void _logSpaces(List spaces, int level) { for (SpaceModel space in spaces) { debugPrint( '${' ' * level}Space: ${space.name}, UUID: ${space.uuid}, Has children: ${space.children.isNotEmpty}'); if (space.children.isNotEmpty) { _logSpaces(space.children, level + 1); } } } void _showCreateCommunityDialog() { showDialog( context: context, builder: (context) => CreateCommunityDialog( onCreateCommunity: (String communityName) { setState(() { debugPrint("hello"); // You can update the communitySpaces map here }); }, ), ); } // Function to filter communities based on the search query Map> _filterCommunities() { if (_searchQuery.isEmpty) { return widget.communitySpaces; } // Filter communities and their spaces based on the search query final filteredCommunitySpaces = >{}; widget.communitySpaces.forEach((communityName, spaces) { if (communityName.toLowerCase().contains(_searchQuery.toLowerCase()) || spaces.any( (space) => _containsQuery(space, _searchQuery.toLowerCase()))) { filteredCommunitySpaces[communityName] = spaces; } }); return filteredCommunitySpaces; } // 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 Flexible( fit: FlexFit .loose, // Ensure the ListView can flex but doesn't expand indefinitely child: ListView( children: filteredCommunities.keys.map((communityName) { return _buildCommunityTile( communityName, filteredCommunities[communityName]!); }).toList(), ), ), ], ), ); } Widget _buildCommunityTile(String communityName, List spaces) { bool hasChildren = spaces.isNotEmpty; debugPrint( 'Building CommunityTile for $communityName, hasChildren: $hasChildren'); return CommunityTile( title: communityName, isExpanded: _expandedTiles[communityName] ?? false, onExpansionChanged: (String title, bool expanded) { debugPrint( 'CommunityTile onExpansionChanged called for $title, expanded: $expanded'); _handleExpansionChange(title, expanded); }, children: hasChildren ? 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 CustomExpansionTile( title: space.name, isExpanded: _expandedTiles[space.uuid] ?? 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) { setState(() { _expandedTiles[uuid] = expanded; debugPrint('_expandedTiles updated: $_expandedTiles'); }); widget.onCommunitySelected?.call(uuid); } }