From 56c4c858bed0144cbe79346addbb2c34ec9cfdf4 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Fri, 11 Oct 2024 13:21:30 +0400 Subject: [PATCH] expansion only on clicking icon --- lib/common/custom_expansion_tile.dart | 84 +++++++++++-------- .../widgets/community_tile.dart | 36 +++----- .../widgets/sidebar_widget.dart | 28 +++++-- .../widgets/space_tile_widget.dart | 1 + 4 files changed, 83 insertions(+), 66 deletions(-) diff --git a/lib/common/custom_expansion_tile.dart b/lib/common/custom_expansion_tile.dart index f637f118..e5dc2e16 100644 --- a/lib/common/custom_expansion_tile.dart +++ b/lib/common/custom_expansion_tile.dart @@ -5,8 +5,10 @@ class CustomExpansionTile extends StatefulWidget { final String title; final List? children; final bool initiallyExpanded; + final bool isSelected; // Add this to track selection final bool? isExpanded; // External control over expansion final ValueChanged? onExpansionChanged; // Notify when expansion changes + final VoidCallback? onItemSelected; // Callback for selecting the item CustomExpansionTile({ required this.title, @@ -14,6 +16,8 @@ class CustomExpansionTile extends StatefulWidget { this.initiallyExpanded = false, this.isExpanded, // Allow external control over expansion this.onExpansionChanged, // Notify when expansion changes + this.onItemSelected, // Trigger item selection when name is tapped + required this.isSelected, // Add this to initialize selection state }); @override @@ -22,7 +26,6 @@ class CustomExpansionTile extends StatefulWidget { class CustomExpansionTileState extends State { bool _isExpanded = false; // Local expansion state - bool _isChecked = false; // Local checkbox state @override void initState() { @@ -51,57 +54,66 @@ class CustomExpansionTileState extends State { Widget build(BuildContext context) { return Column( children: [ - // The main clickable row for the expansion tile - InkWell( - onTap: () { - setState(() { - _isExpanded = !_isExpanded; - widget.onExpansionChanged?.call(_isExpanded); - }); - }, - child: Row( - children: [ - // Checkbox with independent state management - Checkbox( - value: _isChecked, - onChanged: (bool? value) { + Row( + children: [ + // Checkbox with independent state management + Checkbox( + value: false, + onChanged: (bool? value) { + setState(() {}); + }, + side: WidgetStateBorderSide.resolveWith((states) { + return const BorderSide(color: ColorsManager.grayBorder); + }), + fillColor: WidgetStateProperty.resolveWith((states) { + if (states.contains(WidgetState.selected)) { + return ColorsManager.grayBorder; + } else { + return ColorsManager.checkBoxFillColor; + } + }), + checkColor: ColorsManager.whiteColors, + ), + // Expand/collapse icon, now wrapped in a GestureDetector for specific onTap + if (widget.children != null && widget.children!.isNotEmpty) + GestureDetector( + onTap: () { setState(() { - _isChecked = value ?? false; + _isExpanded = !_isExpanded; + widget.onExpansionChanged?.call(_isExpanded); }); }, - side: MaterialStateBorderSide.resolveWith((states) { - return const BorderSide(color: ColorsManager.grayBorder); - }), - fillColor: MaterialStateProperty.resolveWith((states) { - if (states.contains(MaterialState.selected)) { - return ColorsManager.grayBorder; - } else { - return ColorsManager.checkBoxFillColor; - } - }), - checkColor: ColorsManager.whiteColors, - ), - // Show the expand/collapse icon - if (widget.children != null && widget.children!.isNotEmpty) - Icon( + child: Icon( _isExpanded ? Icons.keyboard_arrow_down : Icons.keyboard_arrow_right, color: Colors.grey, size: 16.0, // Adjusted size for better alignment ), - // The title text with dynamic styling - Expanded( + ), + // The title text, wrapped in GestureDetector to handle selection + Expanded( + child: GestureDetector( + onTap: () { + // Triggerxq the onItemSelected callback when the name is tapped + if (widget.onItemSelected != null) { + widget.onItemSelected!(); + } + debugPrint('${widget.title} ${widget.isSelected} tapped for selection'); + }, child: Text( _capitalizeFirstLetter(widget.title), style: TextStyle( - color:ColorsManager.lightGrayColor, + color: widget.isSelected + ? Colors.black // Change color to black when selected + : ColorsManager + .lightGrayColor, // Gray when not selected fontWeight: FontWeight.w400, ), ), ), - ], - ), + ), + ], ), // The expanded section (children) that shows when the tile is expanded if (_isExpanded && diff --git a/lib/pages/spaces_management/widgets/community_tile.dart b/lib/pages/spaces_management/widgets/community_tile.dart index bfc111dc..5b9f79f2 100644 --- a/lib/pages/spaces_management/widgets/community_tile.dart +++ b/lib/pages/spaces_management/widgets/community_tile.dart @@ -1,45 +1,35 @@ import 'package:flutter/material.dart'; import 'package:syncrow_web/common/custom_expansion_tile.dart'; -class CommunityTile extends StatefulWidget { +class CommunityTile extends StatelessWidget { final String title; final List? children; - final bool initiallyExpanded; + final bool isExpanded; + final bool isSelected; final Function(String, bool) onExpansionChanged; + final Function() onItemSelected; const CommunityTile({ super.key, required this.title, - required this.initiallyExpanded, + required this.isExpanded, required this.onExpansionChanged, + required this.onItemSelected, + required this.isSelected, this.children, }); - @override - _CommunityTileState createState() => _CommunityTileState(); -} - -class _CommunityTileState extends State { - late bool _isExpanded; - - @override - void initState() { - super.initState(); - _isExpanded = widget.initiallyExpanded; - } - @override Widget build(BuildContext context) { return CustomExpansionTile( - title: widget.title, - initiallyExpanded: _isExpanded, + title: title, + initiallyExpanded: isExpanded, + isSelected: isSelected, onExpansionChanged: (bool expanded) { - setState(() { - _isExpanded = expanded; - }); - widget.onExpansionChanged(widget.title, expanded); + onExpansionChanged(title, expanded); }, - children: widget.children ?? [], + onItemSelected: onItemSelected, + children: children ?? [], ); } } diff --git a/lib/pages/spaces_management/widgets/sidebar_widget.dart b/lib/pages/spaces_management/widgets/sidebar_widget.dart index bf0164e2..ffc15db2 100644 --- a/lib/pages/spaces_management/widgets/sidebar_widget.dart +++ b/lib/pages/spaces_management/widgets/sidebar_widget.dart @@ -175,20 +175,34 @@ class _SidebarWidgetState extends State { Widget _buildCommunityTile(CommunityModel community) { bool hasChildren = community.spaces.isNotEmpty; - bool isSelectedCommunity = _selectedCommunityUuid == community.uuid; + bool isSelectedCommunity = _selectedCommunityUuid == + community.uuid; // Check if this community is selected - debugPrint( - 'Building CommunityTile for ${community.name}, hasChildren: $hasChildren'); + debugPrint('Building CommunityTile for ${community.name} with UUID: ${community.uuid}'); + debugPrint('Currently selected community UUID: $_selectedCommunityUuid'); + debugPrint('Is selected: $isSelectedCommunity'); + return CommunityTile( title: community.name, - initiallyExpanded: isSelectedCommunity, + isSelected: isSelectedCommunity, + isExpanded: false, + onItemSelected: () { + setState(() { + _selectedSpaceUuid = community.uuid; // Update the selected community + debugPrint( + 'Selected community: ${community.name}, UUID: ${community.uuid}'); + debugPrint( + 'Updated selected community UUID: $_selectedCommunityUuid'); + }); + + if (widget.onCommunitySelected != null) { + widget.onCommunitySelected!(community); // Pass the entire community + } + }, onExpansionChanged: (String title, bool expanded) { debugPrint( 'CommunityTile onExpansionChanged called for $title, expanded: $expanded'); _handleExpansionChange(community.uuid, expanded); - if (widget.onCommunitySelected != null) { - widget.onCommunitySelected!(community); // Pass the entire community - } }, children: hasChildren ? community.spaces.map((space) => _buildSpaceTile(space)).toList() diff --git a/lib/pages/spaces_management/widgets/space_tile_widget.dart b/lib/pages/spaces_management/widgets/space_tile_widget.dart index 98068e7d..e40fc5ce 100644 --- a/lib/pages/spaces_management/widgets/space_tile_widget.dart +++ b/lib/pages/spaces_management/widgets/space_tile_widget.dart @@ -31,6 +31,7 @@ class _SpaceTileState extends State { @override Widget build(BuildContext context) { return CustomExpansionTile( + isSelected: false, title: widget.title, initiallyExpanded: _isExpanded, onExpansionChanged: (bool expanded) {