manually toggleable expandable mod

This commit is contained in:
julian 2022-10-31 11:51:16 -06:00
parent 817460b5e1
commit 3421602ba2

View file

@ -5,6 +5,11 @@ enum ExpandableState {
collapsed, collapsed,
} }
class ExpandableController {
VoidCallback? toggle;
ExpandableState state = ExpandableState.collapsed;
}
class Expandable extends StatefulWidget { class Expandable extends StatefulWidget {
const Expandable({ const Expandable({
Key? key, Key? key,
@ -14,6 +19,7 @@ class Expandable extends StatefulWidget {
this.animation, this.animation,
this.animationDurationMultiplier = 1.0, this.animationDurationMultiplier = 1.0,
this.onExpandChanged, this.onExpandChanged,
this.controller,
}) : super(key: key); }) : super(key: key);
final Widget header; final Widget header;
@ -22,6 +28,7 @@ class Expandable extends StatefulWidget {
final Animation<double>? animation; final Animation<double>? animation;
final double animationDurationMultiplier; final double animationDurationMultiplier;
final void Function(ExpandableState)? onExpandChanged; final void Function(ExpandableState)? onExpandChanged;
final ExpandableController? controller;
@override @override
State<Expandable> createState() => _ExpandableState(); State<Expandable> createState() => _ExpandableState();
@ -31,19 +38,28 @@ class _ExpandableState extends State<Expandable> with TickerProviderStateMixin {
late final AnimationController animationController; late final AnimationController animationController;
late final Animation<double> animation; late final Animation<double> animation;
late final Duration duration; late final Duration duration;
late final ExpandableController? controller;
ExpandableState _toggleState = ExpandableState.collapsed;
Future<void> toggle() async { Future<void> toggle() async {
if (animation.isDismissed) { if (animation.isDismissed) {
await animationController.forward(); await animationController.forward();
widget.onExpandChanged?.call(ExpandableState.collapsed); _toggleState = ExpandableState.collapsed;
widget.onExpandChanged?.call(_toggleState);
} else if (animation.isCompleted) { } else if (animation.isCompleted) {
await animationController.reverse(); await animationController.reverse();
widget.onExpandChanged?.call(ExpandableState.expanded); _toggleState = ExpandableState.expanded;
widget.onExpandChanged?.call(_toggleState);
} }
controller?.state = _toggleState;
} }
@override @override
void initState() { void initState() {
controller = widget.controller;
controller?.toggle = toggle;
duration = Duration( duration = Duration(
milliseconds: (500 * widget.animationDurationMultiplier).toInt(), milliseconds: (500 * widget.animationDurationMultiplier).toInt(),
); );