expansion only on clicking icon

This commit is contained in:
hannathkadher
2024-10-11 13:21:30 +04:00
parent cd957ed1c7
commit 56c4c858be
4 changed files with 83 additions and 66 deletions

View File

@ -5,8 +5,10 @@ class CustomExpansionTile extends StatefulWidget {
final String title; final String title;
final List<Widget>? children; final List<Widget>? children;
final bool initiallyExpanded; final bool initiallyExpanded;
final bool isSelected; // Add this to track selection
final bool? isExpanded; // External control over expansion final bool? isExpanded; // External control over expansion
final ValueChanged<bool>? onExpansionChanged; // Notify when expansion changes final ValueChanged<bool>? onExpansionChanged; // Notify when expansion changes
final VoidCallback? onItemSelected; // Callback for selecting the item
CustomExpansionTile({ CustomExpansionTile({
required this.title, required this.title,
@ -14,6 +16,8 @@ class CustomExpansionTile extends StatefulWidget {
this.initiallyExpanded = false, this.initiallyExpanded = false,
this.isExpanded, // Allow external control over expansion this.isExpanded, // Allow external control over expansion
this.onExpansionChanged, // Notify when expansion changes 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 @override
@ -22,7 +26,6 @@ class CustomExpansionTile extends StatefulWidget {
class CustomExpansionTileState extends State<CustomExpansionTile> { class CustomExpansionTileState extends State<CustomExpansionTile> {
bool _isExpanded = false; // Local expansion state bool _isExpanded = false; // Local expansion state
bool _isChecked = false; // Local checkbox state
@override @override
void initState() { void initState() {
@ -51,29 +54,19 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(
children: [ children: [
// The main clickable row for the expansion tile Row(
InkWell(
onTap: () {
setState(() {
_isExpanded = !_isExpanded;
widget.onExpansionChanged?.call(_isExpanded);
});
},
child: Row(
children: [ children: [
// Checkbox with independent state management // Checkbox with independent state management
Checkbox( Checkbox(
value: _isChecked, value: false,
onChanged: (bool? value) { onChanged: (bool? value) {
setState(() { setState(() {});
_isChecked = value ?? false;
});
}, },
side: MaterialStateBorderSide.resolveWith((states) { side: WidgetStateBorderSide.resolveWith((states) {
return const BorderSide(color: ColorsManager.grayBorder); return const BorderSide(color: ColorsManager.grayBorder);
}), }),
fillColor: MaterialStateProperty.resolveWith((states) { fillColor: WidgetStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) { if (states.contains(WidgetState.selected)) {
return ColorsManager.grayBorder; return ColorsManager.grayBorder;
} else { } else {
return ColorsManager.checkBoxFillColor; return ColorsManager.checkBoxFillColor;
@ -81,27 +74,46 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
}), }),
checkColor: ColorsManager.whiteColors, checkColor: ColorsManager.whiteColors,
), ),
// Show the expand/collapse icon // Expand/collapse icon, now wrapped in a GestureDetector for specific onTap
if (widget.children != null && widget.children!.isNotEmpty) if (widget.children != null && widget.children!.isNotEmpty)
Icon( GestureDetector(
onTap: () {
setState(() {
_isExpanded = !_isExpanded;
widget.onExpansionChanged?.call(_isExpanded);
});
},
child: Icon(
_isExpanded _isExpanded
? Icons.keyboard_arrow_down ? Icons.keyboard_arrow_down
: Icons.keyboard_arrow_right, : Icons.keyboard_arrow_right,
color: Colors.grey, color: Colors.grey,
size: 16.0, // Adjusted size for better alignment size: 16.0, // Adjusted size for better alignment
), ),
// The title text with dynamic styling ),
// The title text, wrapped in GestureDetector to handle selection
Expanded( 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( child: Text(
_capitalizeFirstLetter(widget.title), _capitalizeFirstLetter(widget.title),
style: TextStyle( 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, fontWeight: FontWeight.w400,
), ),
), ),
), ),
],
), ),
],
), ),
// The expanded section (children) that shows when the tile is expanded // The expanded section (children) that shows when the tile is expanded
if (_isExpanded && if (_isExpanded &&

View File

@ -1,45 +1,35 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:syncrow_web/common/custom_expansion_tile.dart'; import 'package:syncrow_web/common/custom_expansion_tile.dart';
class CommunityTile extends StatefulWidget { class CommunityTile extends StatelessWidget {
final String title; final String title;
final List<Widget>? children; final List<Widget>? children;
final bool initiallyExpanded; final bool isExpanded;
final bool isSelected;
final Function(String, bool) onExpansionChanged; final Function(String, bool) onExpansionChanged;
final Function() onItemSelected;
const CommunityTile({ const CommunityTile({
super.key, super.key,
required this.title, required this.title,
required this.initiallyExpanded, required this.isExpanded,
required this.onExpansionChanged, required this.onExpansionChanged,
required this.onItemSelected,
required this.isSelected,
this.children, this.children,
}); });
@override
_CommunityTileState createState() => _CommunityTileState();
}
class _CommunityTileState extends State<CommunityTile> {
late bool _isExpanded;
@override
void initState() {
super.initState();
_isExpanded = widget.initiallyExpanded;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return CustomExpansionTile( return CustomExpansionTile(
title: widget.title, title: title,
initiallyExpanded: _isExpanded, initiallyExpanded: isExpanded,
isSelected: isSelected,
onExpansionChanged: (bool expanded) { onExpansionChanged: (bool expanded) {
setState(() { onExpansionChanged(title, expanded);
_isExpanded = expanded;
});
widget.onExpansionChanged(widget.title, expanded);
}, },
children: widget.children ?? [], onItemSelected: onItemSelected,
children: children ?? [],
); );
} }
} }

View File

@ -175,20 +175,34 @@ class _SidebarWidgetState extends State<SidebarWidget> {
Widget _buildCommunityTile(CommunityModel community) { Widget _buildCommunityTile(CommunityModel community) {
bool hasChildren = community.spaces.isNotEmpty; 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} with UUID: ${community.uuid}');
debugPrint('Currently selected community UUID: $_selectedCommunityUuid');
debugPrint('Is selected: $isSelectedCommunity');
debugPrint(
'Building CommunityTile for ${community.name}, hasChildren: $hasChildren');
return CommunityTile( return CommunityTile(
title: community.name, 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) { onExpansionChanged: (String title, bool expanded) {
debugPrint( debugPrint(
'CommunityTile onExpansionChanged called for $title, expanded: $expanded'); 'CommunityTile onExpansionChanged called for $title, expanded: $expanded');
_handleExpansionChange(community.uuid, expanded); _handleExpansionChange(community.uuid, expanded);
if (widget.onCommunitySelected != null) {
widget.onCommunitySelected!(community); // Pass the entire community
}
}, },
children: hasChildren children: hasChildren
? community.spaces.map((space) => _buildSpaceTile(space)).toList() ? community.spaces.map((space) => _buildSpaceTile(space)).toList()

View File

@ -31,6 +31,7 @@ class _SpaceTileState extends State<SpaceTile> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return CustomExpansionTile( return CustomExpansionTile(
isSelected: false,
title: widget.title, title: widget.title,
initiallyExpanded: _isExpanded, initiallyExpanded: _isExpanded,
onExpansionChanged: (bool expanded) { onExpansionChanged: (bool expanded) {