fixed expand error

This commit is contained in:
hannathkadher
2024-10-08 20:03:31 +04:00
parent 72dfedf2b6
commit 1156af9b8e
6 changed files with 60 additions and 52 deletions

View File

@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/model/community_model.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/model/space_model.dart';
@ -24,16 +25,28 @@ class SpaceManagementBloc
// Fetch all communities // Fetch all communities
List<CommunityModel> communities = await _api.fetchCommunities(); List<CommunityModel> communities = await _api.fetchCommunities();
Map<String, List<SpaceModel>> communitySpaces = {}; // Use Future.wait to handle async calls within map
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _api.getSpaceHierarchy(community.uuid);
for (CommunityModel community in communities) { debugPrint(
// Fetch spaces hierarchy for each community 'Fetched spaces for community ${community.name}: ${spaces.length}');
List<SpaceModel> spaces = await _api.getSpaceHierarchy(community.uuid);
community.spaces = spaces;
communitySpaces[community.uuid] = spaces;
}
emit(SpaceManagementLoaded(communitySpaces: communitySpaces)); return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
updatedAt: community.updatedAt,
name: community.name,
description: community.description,
spaces: spaces, // New spaces list
region: community.region,
);
}).toList(),
);
emit(SpaceManagementLoaded(communities: updatedCommunities));
} catch (e) { } catch (e) {
emit(SpaceManagementError('Error loading communities and spaces: $e')); emit(SpaceManagementError('Error loading communities and spaces: $e'));
} }

View File

@ -1,4 +1,5 @@
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.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/model/space_model.dart';
abstract class SpaceManagementState extends Equatable { abstract class SpaceManagementState extends Equatable {
@ -13,12 +14,13 @@ class SpaceManagementInitial extends SpaceManagementState {}
class SpaceManagementLoading extends SpaceManagementState {} class SpaceManagementLoading extends SpaceManagementState {}
class SpaceManagementLoaded extends SpaceManagementState { class SpaceManagementLoaded extends SpaceManagementState {
final Map<String, List<SpaceModel>> communitySpaces; final List<CommunityModel> communities;
const SpaceManagementLoaded({required this.communitySpaces});
const SpaceManagementLoaded({ required this.communities});
@override @override
List<Object> get props => [communitySpaces]; List<Object> get props => [communities];
} }
class SpaceCreationSuccess extends SpaceManagementState {} class SpaceCreationSuccess extends SpaceManagementState {}

View File

@ -13,7 +13,7 @@ class SpaceModel {
final SpaceModel? parent; final SpaceModel? parent;
final CommunityModel? community; final CommunityModel? community;
final List<SpaceModel> children; final List<SpaceModel> children;
final String icon; final String? icon;
Offset position; Offset position;
bool isHovered; bool isHovered;
@ -54,11 +54,11 @@ class SpaceModel {
.map((child) => SpaceModel.fromJson(child)) .map((child) => SpaceModel.fromJson(child))
.toList() .toList()
: [], : [],
icon: json['icon'], // New field from JSON icon: json['icon'],
position: json['position'] != null position: json['position'] != null
? Offset(json['position']['dx'], json['position']['dy']) ? Offset(json['position']['dx'], json['position']['dy'])
: Offset(0, 0), // Default position if not provided in JSON : const Offset(0, 0),
isHovered: json['isHovered'] ?? false, // Default isHovered if not in JSON isHovered: false,
); );
} }

View File

@ -4,6 +4,7 @@ import 'package:syncrow_web/pages/common/buttons/add_space_button.dart';
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart';
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.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/model/space_model.dart';
import 'package:syncrow_web/pages/spaces_management/view/curved_line_painter.dart'; import 'package:syncrow_web/pages/spaces_management/view/curved_line_painter.dart';
import 'package:syncrow_web/pages/spaces_management/view/dialogs/create_space_dialog.dart'; import 'package:syncrow_web/pages/spaces_management/view/dialogs/create_space_dialog.dart';
@ -53,8 +54,7 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
if (state is SpaceManagementLoading) { if (state is SpaceManagementLoading) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} else if (state is SpaceManagementLoaded) { } else if (state is SpaceManagementLoaded) {
return _buildLoadedState( return _buildLoadedState(context, screenSize, state.communities);
context, screenSize, state.communitySpaces);
} else if (state is SpaceManagementError) { } else if (state is SpaceManagementError) {
return Center(child: Text('Error: ${state.errorMessage}')); return Center(child: Text('Error: ${state.errorMessage}'));
} }
@ -65,24 +65,22 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
); );
} }
Widget _buildLoadedState(BuildContext context, Size screenSize, Widget _buildLoadedState(
Map<String, List<SpaceModel>> communitySpaces) { BuildContext context, Size screenSize, List<CommunityModel> communities) {
return Stack( return Stack(
clipBehavior: Clip.none, clipBehavior: Clip.none,
children: [ children: [
Row( Row(
children: [ children: [
SidebarWidget( SidebarWidget(
communitySpaces: communitySpaces, communities: communities,
onCommunitySelected: (community) { onCommunitySelected: (community) {
context.read<SpaceManagementBloc>().add( context.read<SpaceManagementBloc>().add(
LoadCommunityAndSpacesEvent(), // Re-fetch or perform community-specific actions LoadCommunityAndSpacesEvent(), // Re-fetch or perform community-specific actions
); );
}, },
), ),
Expanded( _buildCommunityStructureArea(context, screenSize),
child: _buildCommunityStructureArea(context, screenSize),
),
], ],
), ),
_buildGradientBorder(), _buildGradientBorder(),

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import 'package:syncrow_web/common/custom_expansion_tile.dart'; import 'package:syncrow_web/common/custom_expansion_tile.dart';
import 'package:syncrow_web/common/search_bar.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/model/space_model.dart';
import 'package:syncrow_web/pages/spaces_management/widgets/community_tile.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/view/dialogs/create_community_dialog.dart';
@ -11,9 +12,9 @@ import 'package:syncrow_web/utils/style.dart';
class SidebarWidget extends StatefulWidget { class SidebarWidget extends StatefulWidget {
final Function(String)? onCommunitySelected; final Function(String)? onCommunitySelected;
final Map<String, List<SpaceModel>> communitySpaces; final List<CommunityModel> communities;
SidebarWidget({this.onCommunitySelected, required this.communitySpaces}); SidebarWidget({this.onCommunitySelected, required this.communities});
@override @override
_SidebarWidgetState createState() => _SidebarWidgetState(); _SidebarWidgetState createState() => _SidebarWidgetState();
@ -34,9 +35,7 @@ class _SidebarWidgetState extends State<SidebarWidget> {
builder: (context) => CreateCommunityDialog( builder: (context) => CreateCommunityDialog(
onCreateCommunity: (String communityName) { onCreateCommunity: (String communityName) {
setState(() { setState(() {
debugPrint("hello"); // You can update the community list here when a new community is added
// You can update the communitySpaces map here
}); });
}, },
), ),
@ -44,21 +43,19 @@ class _SidebarWidgetState extends State<SidebarWidget> {
} }
// Function to filter communities based on the search query // Function to filter communities based on the search query
Map<String, List<SpaceModel>> _filterCommunities() { List<CommunityModel> _filterCommunities() {
if (_searchQuery.isEmpty) { if (_searchQuery.isEmpty) {
return widget.communitySpaces; return widget.communities;
} }
// Filter communities and their spaces based on the search query // Filter communities and their spaces based on the search query
final filteredCommunitySpaces = <String, List<SpaceModel>>{}; return widget.communities.where((community) {
widget.communitySpaces.forEach((communityName, spaces) { final containsQueryInCommunity =
if (communityName.toLowerCase().contains(_searchQuery.toLowerCase()) || community.name.toLowerCase().contains(_searchQuery.toLowerCase());
spaces.any( final containsQueryInSpaces = community.spaces
(space) => _containsQuery(space, _searchQuery.toLowerCase()))) { .any((space) => _containsQuery(space, _searchQuery.toLowerCase()));
filteredCommunitySpaces[communityName] = spaces; return containsQueryInCommunity || containsQueryInSpaces;
} }).toList();
});
return filteredCommunitySpaces;
} }
// 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
@ -128,13 +125,10 @@ class _SidebarWidgetState extends State<SidebarWidget> {
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
// Community list // Community list
Flexible( Expanded(
fit: FlexFit
.loose, // Ensure the ListView can flex but doesn't expand indefinitely
child: ListView( child: ListView(
children: filteredCommunities.keys.map((communityName) { children: filteredCommunities.map((community) {
return _buildCommunityTile( return _buildCommunityTile(community);
communityName, filteredCommunities[communityName]!);
}).toList(), }).toList(),
), ),
), ),
@ -143,21 +137,21 @@ class _SidebarWidgetState extends State<SidebarWidget> {
); );
} }
Widget _buildCommunityTile(String communityName, List<SpaceModel> spaces) { Widget _buildCommunityTile(CommunityModel community) {
bool hasChildren = spaces.isNotEmpty; bool hasChildren = community.spaces.isNotEmpty;
debugPrint( debugPrint(
'Building CommunityTile for $communityName, hasChildren: $hasChildren'); 'Building CommunityTile for ${community.name}, hasChildren: $hasChildren');
return CommunityTile( return CommunityTile(
title: communityName, title: community.name,
isExpanded: _expandedTiles[communityName] ?? false, isExpanded: _expandedTiles[community.uuid] ?? false,
onExpansionChanged: (String title, bool expanded) { onExpansionChanged: (String title, bool expanded) {
debugPrint( debugPrint(
'CommunityTile onExpansionChanged called for $title, expanded: $expanded'); 'CommunityTile onExpansionChanged called for $title, expanded: $expanded');
_handleExpansionChange(title, expanded); _handleExpansionChange(community.uuid, expanded);
}, },
children: hasChildren children: hasChildren
? spaces.map((space) => _buildSpaceTile(space)).toList() ? community.spaces.map((space) => _buildSpaceTile(space)).toList()
: null, // Render spaces within the community : null, // Render spaces within the community
); );
} }

View File

@ -223,4 +223,5 @@ class CommunitySpaceManagementApi {
return []; return [];
} }
} }
} }