-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexpandable_widget.dart
More file actions
105 lines (82 loc) · 2.45 KB
/
expandable_widget.dart
File metadata and controls
105 lines (82 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import 'package:flutter/material.dart';
class ExpandableWidget extends StatefulWidget {
final Widget header;
final Widget child;
final Curve curve;
final int duration;
const ExpandableWidget(
{
super.key,
required this.header,
required this.child,
this.curve = Curves.fastLinearToSlowEaseIn,
this.duration = 200
}
);
@override
State<ExpandableWidget> createState() => _ExpandableWidgetState();
}
class _ExpandableWidgetState extends State<ExpandableWidget> with TickerProviderStateMixin {
late final AnimationController _headerAnimationController;
late final Animation<double> _headerAnimation;
late final AnimationController _childAnimationController;
late final Animation<double> _childAnimation;
late final Tween<double> _sizeTween = Tween(begin: 0, end: 1);
var isExpanded = false;
var isFirstTap = true;
@override
void initState() {
_headerAnimationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: widget.duration)
);
_headerAnimation = CurvedAnimation(
parent: _headerAnimationController,
curve: widget.curve
);
_childAnimationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: widget.duration)
);
_childAnimation = CurvedAnimation(
parent: _childAnimationController,
curve: widget.curve
);
super.initState();
}
@override
void dispose() {
_headerAnimationController.dispose();
_childAnimationController.dispose();
super.dispose();
}
void _expandOnChanged() { /// This method is triggered on tap
isFirstTap = false;
setState(() {
isExpanded = !isExpanded;
});
isExpanded ?
_headerAnimationController.forward() : _headerAnimationController.reverse();
isExpanded ?
_childAnimationController.reverse() : _childAnimationController.forward();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
GestureDetector(
onTap: _expandOnChanged,
child: isFirstTap ? widget.header : !isExpanded ?
SizeTransition(
sizeFactor: _sizeTween.animate(_childAnimation),
child: widget.header,
) :
SizeTransition(
sizeFactor: _sizeTween.animate(_headerAnimation),
child: widget.child,
),
)
],
);
}
}