fixed re-rendering of community widget

This commit is contained in:
hannathkadher
2024-10-08 20:37:04 +04:00
parent 1156af9b8e
commit 33712b7690
4 changed files with 90 additions and 30 deletions

View File

@ -5,8 +5,8 @@ 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? isExpanded; // New parameter to control expansion final bool? isExpanded; // External control over expansion
final ValueChanged<bool>? onExpansionChanged; // Callback for expansion change final ValueChanged<bool>? onExpansionChanged; // Notify when expansion changes
CustomExpansionTile({ CustomExpansionTile({
required this.title, required this.title,
@ -21,8 +21,8 @@ class CustomExpansionTile extends StatefulWidget {
} }
class CustomExpansionTileState extends State<CustomExpansionTile> { class CustomExpansionTileState extends State<CustomExpansionTile> {
bool _isExpanded = false; bool _isExpanded = false; // Local expansion state
bool _isChecked = false; bool _isChecked = false; // Local checkbox state
@override @override
void initState() { void initState() {
@ -33,6 +33,7 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
@override @override
void didUpdateWidget(CustomExpansionTile oldWidget) { void didUpdateWidget(CustomExpansionTile oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
// Sync local state with external control of expansion state
if (widget.isExpanded != null && widget.isExpanded != _isExpanded) { if (widget.isExpanded != null && widget.isExpanded != _isExpanded) {
setState(() { setState(() {
_isExpanded = widget.isExpanded!; _isExpanded = widget.isExpanded!;
@ -40,6 +41,7 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
} }
} }
// Utility function to capitalize the first letter of the title
String _capitalizeFirstLetter(String text) { String _capitalizeFirstLetter(String text) {
if (text.isEmpty) return text; if (text.isEmpty) return text;
return text[0].toUpperCase() + text.substring(1); return text[0].toUpperCase() + text.substring(1);
@ -49,6 +51,7 @@ 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
InkWell( InkWell(
onTap: () { onTap: () {
setState(() { setState(() {
@ -58,7 +61,7 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
}, },
child: Row( child: Row(
children: [ children: [
// Customizing the Checkbox // Checkbox with independent state management
Checkbox( Checkbox(
value: _isChecked, value: _isChecked,
onChanged: (bool? value) { onChanged: (bool? value) {
@ -66,11 +69,11 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
_isChecked = value ?? false; _isChecked = value ?? false;
}); });
}, },
side: WidgetStateBorderSide.resolveWith((states) { side: MaterialStateBorderSide.resolveWith((states) {
return const BorderSide(color: ColorsManager.grayBorder); return const BorderSide(color: ColorsManager.grayBorder);
}), }),
fillColor: WidgetStateProperty.resolveWith((states) { fillColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(WidgetState.selected)) { if (states.contains(MaterialState.selected)) {
return ColorsManager.grayBorder; return ColorsManager.grayBorder;
} else { } else {
return ColorsManager.checkBoxFillColor; return ColorsManager.checkBoxFillColor;
@ -78,15 +81,16 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
}), }),
checkColor: ColorsManager.whiteColors, checkColor: ColorsManager.whiteColors,
), ),
// Show the expand/collapse icon
if (widget.children != null && widget.children!.isNotEmpty) if (widget.children != null && widget.children!.isNotEmpty)
Icon( 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, // Reduced icon size size: 16.0, // Adjusted size for better alignment
), ),
// Title Text // The title text with dynamic styling
Expanded( Expanded(
child: Text( child: Text(
_capitalizeFirstLetter(widget.title), _capitalizeFirstLetter(widget.title),
@ -101,11 +105,12 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
], ],
), ),
), ),
// The expanded section (children) that shows when the tile is expanded
if (_isExpanded && if (_isExpanded &&
widget.children != null && widget.children != null &&
widget.children!.isNotEmpty) widget.children!.isNotEmpty)
Padding( Padding(
padding: const EdgeInsets.only(left: 48.0), // Indent the children padding: const EdgeInsets.only(left: 48.0), // Indented children
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: widget.children!, children: widget.children!,

View File

@ -1,29 +1,45 @@
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 StatelessWidget { class CommunityTile extends StatefulWidget {
final String title; final String title;
final bool isExpanded;
final Function(String, bool) onExpansionChanged;
final List<Widget>? children; final List<Widget>? children;
final bool initiallyExpanded;
final Function(String, bool) onExpansionChanged;
const CommunityTile({ const CommunityTile({
Key? key, Key? key,
required this.title, required this.title,
required this.isExpanded, required this.initiallyExpanded,
required this.onExpansionChanged, required this.onExpansionChanged,
this.children, this.children,
}) : super(key: key); }) : super(key: key);
@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: title, title: widget.title,
initiallyExpanded: isExpanded, initiallyExpanded: _isExpanded,
onExpansionChanged: (bool expanded) { onExpansionChanged: (bool expanded) {
onExpansionChanged(title, expanded); setState(() {
_isExpanded = expanded;
});
widget.onExpansionChanged(widget.title, expanded);
}, },
children: children ?? [], children: widget.children ?? [],
); );
} }
} }

View File

@ -1,11 +1,11 @@
import 'package:flutter/material.dart'; 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/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/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';
import 'package:syncrow_web/pages/spaces_management/widgets/space_tile_widget.dart';
import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/style.dart'; import 'package:syncrow_web/utils/style.dart';
@ -144,7 +144,7 @@ class _SidebarWidgetState extends State<SidebarWidget> {
'Building CommunityTile for ${community.name}, hasChildren: $hasChildren'); 'Building CommunityTile for ${community.name}, hasChildren: $hasChildren');
return CommunityTile( return CommunityTile(
title: community.name, title: community.name,
isExpanded: _expandedTiles[community.uuid] ?? false, initiallyExpanded: 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');
@ -159,9 +159,9 @@ class _SidebarWidgetState extends State<SidebarWidget> {
Widget _buildSpaceTile(SpaceModel space) { Widget _buildSpaceTile(SpaceModel space) {
debugPrint( debugPrint(
'Building SpaceTile for ${space.name}, hasChildren: ${space.children.isNotEmpty}'); 'Building SpaceTile for ${space.name}, hasChildren: ${space.children.isNotEmpty}');
return CustomExpansionTile( return SpaceTile(
title: space.name, title: space.name,
isExpanded: _expandedTiles[space.uuid] ?? false, initiallyExpanded: false,
onExpansionChanged: (bool expanded) { onExpansionChanged: (bool expanded) {
debugPrint( debugPrint(
'SpaceTile onExpansionChanged called for ${space.name}, expanded: $expanded'); 'SpaceTile onExpansionChanged called for ${space.name}, expanded: $expanded');
@ -175,11 +175,5 @@ class _SidebarWidgetState extends State<SidebarWidget> {
); );
} }
void _handleExpansionChange(String uuid, bool expanded) { void _handleExpansionChange(String uuid, bool expanded) {}
setState(() {
_expandedTiles[uuid] = expanded;
debugPrint('_expandedTiles updated: $_expandedTiles');
});
widget.onCommunitySelected?.call(uuid);
}
} }

View File

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