Files
syncrow-web/lib/pages/spaces_management/widgets/sidebar_widget.dart
hannathkadher 72dfedf2b6 removed logs
2024-10-08 13:54:24 +04:00

192 lines
6.3 KiB
Dart

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<String, List<SpaceModel>> communitySpaces;
SidebarWidget({this.onCommunitySelected, required this.communitySpaces});
@override
_SidebarWidgetState createState() => _SidebarWidgetState();
}
class _SidebarWidgetState extends State<SidebarWidget> {
String _searchQuery = ''; // Track search query
Map<String, bool> _expandedTiles = {}; // Track expanded state for each UUID
@override
void initState() {
super.initState();
}
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<String, List<SpaceModel>> _filterCommunities() {
if (_searchQuery.isEmpty) {
return widget.communitySpaces;
}
// Filter communities and their spaces based on the search query
final filteredCommunitySpaces = <String, List<SpaceModel>>{};
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<SpaceModel> 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);
}
}