Skip to content

Instantly share code, notes, and snippets.

@venkatd
Last active April 18, 2023 00:33
Show Gist options
  • Save venkatd/b820b3b34b09239d85bf51ccb7f2278a to your computer and use it in GitHub Desktop.
Save venkatd/b820b3b34b09239d85bf51ccb7f2278a 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