reduce duplicated code

This commit is contained in:
hannathkadher
2024-09-10 22:57:34 +04:00
parent b362029424
commit b40998558e
7 changed files with 248 additions and 162 deletions
+21 -13
View File
@@ -5,11 +5,15 @@ class CustomExpansionTile extends StatefulWidget {
final String title;
final List<Widget>? children;
final bool initiallyExpanded;
final bool? isExpanded; // New parameter to control expansion
final ValueChanged<bool>? onExpansionChanged; // Callback for expansion change
CustomExpansionTile({
required this.title,
this.children,
this.initiallyExpanded = false,
this.isExpanded, // Allow external control over expansion
this.onExpansionChanged, // Notify when expansion changes
});
@override
@@ -26,6 +30,16 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
_isExpanded = widget.initiallyExpanded;
}
@override
void didUpdateWidget(CustomExpansionTile oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.isExpanded != null && widget.isExpanded != _isExpanded) {
setState(() {
_isExpanded = widget.isExpanded!;
});
}
}
@override
Widget build(BuildContext context) {
return Column(
@@ -34,6 +48,7 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
onTap: () {
setState(() {
_isExpanded = !_isExpanded;
widget.onExpansionChanged?.call(_isExpanded);
});
},
child: Row(
@@ -47,28 +62,22 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
});
},
side: WidgetStateBorderSide.resolveWith((states) {
if (states.contains(WidgetState.selected)) {
return const BorderSide(color: ColorsManager.grayBorder);
}
return const BorderSide(color: ColorsManager.grayBorder);
}),
// Customize the color for different checkbox states
fillColor: WidgetStateProperty.resolveWith((states) {
if (states.contains(WidgetState.selected)) {
return ColorsManager.grayBorder; // Checked state color
return ColorsManager.grayBorder;
} else {
return ColorsManager.checkBoxFillColor;
}
return ColorsManager
.checkBoxFillColor; // Unchecked state color
}),
checkColor: ColorsManager.whiteColors, // Checkmark color
checkColor: ColorsManager.whiteColors,
),
// Expand/Collapse Icon with reduced size
if (widget.children != null && widget.children!.isNotEmpty)
Icon(
_isExpanded
? Icons.keyboard_arrow_down // Upward arrow when expanded
: Icons
.keyboard_arrow_right, // Right arrow when collapsed
? Icons.keyboard_arrow_down
: Icons.keyboard_arrow_right,
color: Colors.grey,
size: 16.0, // Reduced icon size
),
@@ -87,7 +96,6 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
],
),
),
// Display children if available and expanded
if (_isExpanded &&
widget.children != null &&
widget.children!.isNotEmpty)