diff --git a/lib/pages/spaces_management/widgets/community_structure_widget.dart b/lib/pages/spaces_management/widgets/community_structure_widget.dart index 90937f28..a1e295e3 100644 --- a/lib/pages/spaces_management/widgets/community_structure_widget.dart +++ b/lib/pages/spaces_management/widgets/community_structure_widget.dart @@ -52,7 +52,8 @@ class _CommunityStructureAreaState extends State { void initState() { super.initState(); spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : []; - connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : []; + connections = + widget.spaces.isNotEmpty ? createConnections(widget.spaces) : []; _adjustCanvasSizeForSpaces(); _nameController = TextEditingController( text: widget.selectedCommunity?.name ?? '', @@ -79,12 +80,14 @@ class _CommunityStructureAreaState extends State { if (oldWidget.spaces != widget.spaces) { setState(() { spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : []; - connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : []; + connections = + widget.spaces.isNotEmpty ? createConnections(widget.spaces) : []; _adjustCanvasSizeForSpaces(); }); } - if (widget.selectedSpace != oldWidget.selectedSpace && widget.selectedSpace != null) { + if (widget.selectedSpace != oldWidget.selectedSpace && + widget.selectedSpace != null) { WidgetsBinding.instance.addPostFrameCallback((_) { _moveToSpace(widget.selectedSpace!); }); @@ -101,7 +104,7 @@ class _CommunityStructureAreaState extends State { return Expanded( child: GestureDetector( onTap: () { - _deselectSpace(); + _deselectSpace(context); }, child: Container( decoration: const BoxDecoration( @@ -156,9 +159,11 @@ class _CommunityStructureAreaState extends State { children: [ for (var connection in connections) Opacity( - opacity: - _isHighlightedConnection(connection) ? 1.0 : 0.3, // Adjust opacity - child: CustomPaint(painter: CurvedLinePainter([connection])), + opacity: _isHighlightedConnection(connection) + ? 1.0 + : 0.3, // Adjust opacity + child: CustomPaint( + painter: CurvedLinePainter([connection])), ), for (var entry in spaces.asMap().entries) if (entry.value.status != SpaceStatus.deleted) @@ -167,10 +172,12 @@ class _CommunityStructureAreaState extends State { top: entry.value.position.dy, child: SpaceCardWidget( index: entry.key, - onButtonTap: (int index, Offset newPosition, String direction) { + onButtonTap: (int index, Offset newPosition, + String direction) { _showCreateSpaceDialog( screenSize, - position: spaces[index].position + newPosition, + position: + spaces[index].position + newPosition, parentIndex: index, direction: direction, ); @@ -183,7 +190,8 @@ class _CommunityStructureAreaState extends State { _updateNodePosition(entry.value, newPosition); }, buildSpaceContainer: (int index) { - final bool isHighlighted = _isHighlightedSpace(spaces[index]); + final bool isHighlighted = + _isHighlightedSpace(spaces[index]); return Opacity( opacity: isHighlighted ? 1.0 : 0.3, @@ -193,7 +201,7 @@ class _CommunityStructureAreaState extends State { _showEditSpaceDialog(spaces[index]); }, onTap: () { - _selectSpace(spaces[index]); + _selectSpace(context, spaces[index]); }, icon: spaces[index].icon ?? '', name: spaces[index].name, @@ -210,7 +218,8 @@ class _CommunityStructureAreaState extends State { child: AddSpaceButton( onTap: () { _showCreateSpaceDialog(screenSize, - canvasHeight: canvasHeight, canvasWidth: canvasWidth); + canvasHeight: canvasHeight, + canvasWidth: canvasWidth); }, ), ), @@ -270,12 +279,14 @@ class _CommunityStructureAreaState extends State { builder: (BuildContext context) { return CreateSpaceDialog( products: widget.products, - parentSpace: parentIndex != null? spaces[parentIndex] : null, - onCreateSpace: (String name, String icon, List selectedProducts) { + parentSpace: parentIndex != null ? spaces[parentIndex] : null, + onCreateSpace: (String name, String icon, + List selectedProducts) { setState(() { // Set the first space in the center or use passed position - Offset centerPosition = position ?? _getCenterPosition(screenSize); + Offset centerPosition = + position ?? _getCenterPosition(screenSize); SpaceModel newSpace = SpaceModel( name: name, icon: icon, @@ -319,7 +330,8 @@ class _CommunityStructureAreaState extends State { icon: space.icon, isEdit: true, selectedProducts: space.selectedProducts, - onCreateSpace: (String name, String icon, List selectedProducts) { + onCreateSpace: (String name, String icon, + List selectedProducts) { setState(() { // Update the space's properties space.name = name; @@ -331,8 +343,7 @@ class _CommunityStructureAreaState extends State { } }); }, - // Pre-fill the dialog with current space data - key: Key(space.name), // Add a unique key to ensure dialog refresh + key: Key(space.name), ); }, ); @@ -465,20 +476,19 @@ class _CommunityStructureAreaState extends State { ..scale(1.2); } - void _selectSpace(SpaceModel space) { - setState(() { - widget.selectedSpace = space; - }); - - if (widget.onSpaceSelected != null) { - widget.onSpaceSelected!(space); - } + void _selectSpace(BuildContext context, SpaceModel space) { + context.read().add( + SelectSpaceEvent( + selectedCommunity: widget.selectedCommunity, + selectedSpace: space), + ); } bool _isHighlightedSpace(SpaceModel space) { if (widget.selectedSpace == null) return true; if (space == widget.selectedSpace) return true; - if (widget.selectedSpace?.parent?.internalId == space.internalId) return true; + if (widget.selectedSpace?.parent?.internalId == space.internalId) + return true; if (widget.selectedSpace?.children != null) { for (var child in widget.selectedSpace!.children) { if (child.internalId == space.internalId) { @@ -489,16 +499,11 @@ class _CommunityStructureAreaState extends State { return false; } - void _deselectSpace() { - if (widget.selectedSpace != null) { - setState(() { - widget.selectedSpace = null; - }); - - if (widget.onSpaceSelected != null) { - widget.onSpaceSelected!(null); // Notify parent that no space is selected - } - } + void _deselectSpace(BuildContext context) { + context.read().add( + SelectSpaceEvent( + selectedCommunity: widget.selectedCommunity, selectedSpace: null), + ); } bool _isHighlightedConnection(Connection connection) {