Skip to content

Commit

Permalink
Ready for review
Browse files Browse the repository at this point in the history
  • Loading branch information
Daviiddoo committed Oct 1, 2024
1 parent 3c3dd5e commit 01b79c3
Show file tree
Hide file tree
Showing 3 changed files with 302 additions and 170 deletions.
29 changes: 23 additions & 6 deletions forui/lib/src/widgets/accordion/accordion_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ class FAccordionController extends ChangeNotifier {
final int _min;
final int? _max;

/// An [FAccordionController] that allows only one section to be expanded at a time.
/// Creates an [FAccordionController] that allows only one section to be expanded at a time.
factory FAccordionController.radio({Duration? animationDuration}) => FAccordionController(
max: 1,
animationDuration: animationDuration ?? const Duration(milliseconds: 200),
);

/// Creates a [FAccordionController].
///
/// The [min] and [max] values are the minimum and maximum number of selections allowed. Defaults to no minimum and maximum.
/// The [min], inclusive, and [max], inclusive, values are the minimum and maximum number of selections allowed.
/// Defaults to no minimum and maximum.
///
/// # Contract:
/// * Throws [AssertionError] if [min] < 0.
Expand All @@ -28,7 +29,7 @@ class FAccordionController extends ChangeNotifier {
FAccordionController({
int min = 0,
int? max,
this.animationDuration = const Duration(milliseconds: 100),
this.animationDuration = const Duration(milliseconds: 200),
}) : _min = min,
_max = max,
controllers = {},
Expand All @@ -38,7 +39,12 @@ class FAccordionController extends ChangeNotifier {
assert(max == null || min <= max, 'The max value must be greater than or equal to the min value.');

/// Adds an item to the accordion.
void addItem(int index, AnimationController controller, Animation animation, {required bool initiallyExpanded}) {
Future<void> addItem(
int index,
AnimationController controller,
Animation animation, {
required bool initiallyExpanded,
}) async {
controller
..value = initiallyExpanded ? 1 : 0
..duration = animationDuration;
Expand All @@ -47,14 +53,19 @@ class FAccordionController extends ChangeNotifier {

if (initiallyExpanded) {
if (_max != null && _expanded.length >= _max) {
return;
if (!await _collapse(expanded.first)) {
return;
}
}
_expanded.add(index);
}
}

/// Removes the item at the given [index] from the accordion. Returns true if the item was removed.
bool removeItem(int index) {
if (_expanded.length <= _min && _expanded.contains(index)) {
return false;
}
final removed = controllers.remove(index);
_expanded.remove(index);
return removed != null;
Expand All @@ -77,7 +88,7 @@ class FAccordionController extends ChangeNotifier {
///
/// This method should typically not be called while the widget tree is being rebuilt.
Future<void> expand(int index) async {
if (_expanded.contains(index)) {
if (_expanded.contains(index) || controllers[index] == null) {
return;
}

Expand Down Expand Up @@ -122,4 +133,10 @@ class FAccordionController extends ChangeNotifier {

/// The currently selected values.
Set<int> get expanded => {..._expanded};

/// Removes all objects from the expanded and controller list;
void clear() {
_expanded.clear();
controllers.clear();
}
}
4 changes: 2 additions & 2 deletions forui/lib/src/widgets/accordion/accordion_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class _FAccordionItemState extends State<FAccordionItem> with TickerProviderStat
late Animation<double> _expand;

@override
void didChangeDependencies() {
Future<void> didChangeDependencies() async {
super.didChangeDependencies();
final data = FAccordionItemData.of(context);

Expand All @@ -69,7 +69,7 @@ class _FAccordionItemState extends State<FAccordionItem> with TickerProviderStat
parent: _controller,
),
);
data.controller.addItem(data.index, _controller, _expand, initiallyExpanded: widget.initiallyExpanded);
await data.controller.addItem(data.index, _controller, _expand, initiallyExpanded: widget.initiallyExpanded);
}

@override
Expand Down
Loading

0 comments on commit 01b79c3

Please sign in to comment.