Skip to content

Instantly share code, notes, and snippets.

@AnthoniG
Forked from venkatd/expandable_section.dart
Created August 3, 2022 07:53
Show Gist options
  • Save AnthoniG/6fa790dd568db0d87a949e363e132252 to your computer and use it in GitHub Desktop.
Save AnthoniG/6fa790dd568db0d87a949e363e132252 to your computer and use it in GitHub Desktop.
import 'package:flutter/widgets.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
// adapted from https://stackoverflow.com/a/54173729/67655
class ExpandableSection extends HookWidget {
const ExpandableSection({
Key key,
this.expanded = false,
this.child,
this.animationDuration = const Duration(milliseconds: 500),
this.animationCurve = Curves.fastOutSlowIn,
}) : assert(expanded != null),
assert(child != null),
assert(animationDuration != null),
assert(animationCurve != null),
super(key: key);
final bool expanded;
final Widget child;
final Duration animationDuration;
final Curve animationCurve;
@override
Widget build(BuildContext context) {
final controller = useAnimationController(
duration: animationDuration,
initialValue: expanded ? 1.0 : 0.0,
);
useEffect(() {
if (expanded && controller.value < 1.0) {
controller.forward();
} else if (!expanded && controller.value > 0.0) {
controller.reverse();
}
return null;
});
return SizeTransition(
axisAlignment: 1.0,
sizeFactor: CurvedAnimation(
parent: controller,
curve: animationCurve,
),
child: child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment