mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-01-30 06:06:45 +00:00
simple expandable widget
This commit is contained in:
parent
04fcc58f4a
commit
e2bd064ba5
1 changed files with 91 additions and 0 deletions
91
lib/widgets/expandable.dart
Normal file
91
lib/widgets/expandable.dart
Normal file
|
@ -0,0 +1,91 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
enum ExpandableState {
|
||||
expanded,
|
||||
collapsed,
|
||||
}
|
||||
|
||||
class Expandable extends StatefulWidget {
|
||||
const Expandable({
|
||||
Key? key,
|
||||
required this.header,
|
||||
required this.body,
|
||||
this.animationController,
|
||||
this.animation,
|
||||
this.animationDurationMultiplier = 1.0,
|
||||
this.onExpandChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
final Widget header;
|
||||
final Widget body;
|
||||
final AnimationController? animationController;
|
||||
final Animation<double>? animation;
|
||||
final double animationDurationMultiplier;
|
||||
final void Function(ExpandableState)? onExpandChanged;
|
||||
|
||||
@override
|
||||
State<Expandable> createState() => _ExpandableState();
|
||||
}
|
||||
|
||||
class _ExpandableState extends State<Expandable> with TickerProviderStateMixin {
|
||||
late final AnimationController animationController;
|
||||
late final Animation<double> animation;
|
||||
late final Duration duration;
|
||||
|
||||
Future<void> toggle() async {
|
||||
if (animation.isDismissed) {
|
||||
await animationController.forward();
|
||||
widget.onExpandChanged?.call(ExpandableState.collapsed);
|
||||
} else if (animation.isCompleted) {
|
||||
await animationController.reverse();
|
||||
widget.onExpandChanged?.call(ExpandableState.expanded);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
duration = Duration(
|
||||
milliseconds: (500 * widget.animationDurationMultiplier).toInt(),
|
||||
);
|
||||
animationController = widget.animationController ??
|
||||
AnimationController(
|
||||
vsync: this,
|
||||
duration: duration,
|
||||
);
|
||||
animation = widget.animation ??
|
||||
Tween<double>(begin: 0.0, end: 1.0).animate(
|
||||
CurvedAnimation(
|
||||
curve: Curves.easeInOut,
|
||||
parent: animationController,
|
||||
),
|
||||
);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
animationController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: toggle,
|
||||
child: Container(
|
||||
color: Colors.transparent,
|
||||
child: widget.header,
|
||||
),
|
||||
),
|
||||
SizeTransition(
|
||||
sizeFactor: animation,
|
||||
axisAlignment: 1.0,
|
||||
child: widget.body,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue