Skip to content

Commit 2cd61bc

Browse files
committed
feat: Improved ExpandListTile widget with an animation.
1 parent 9452fc7 commit 2cd61bc

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

lib/widgets/list/expand_list_tile.dart

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,26 @@ class ExpandListTile extends StatefulWidget {
2929
}
3030

3131
/// The expand list tile instance.
32-
class _ExpandListTileState extends State<ExpandListTile> with BrightnessListener {
32+
class _ExpandListTileState extends State<ExpandListTile> with SingleTickerProviderStateMixin, BrightnessListener {
33+
/// The expand controller.
34+
late AnimationController expandController;
35+
36+
/// The expand animation.
37+
late Animation<double> expandAnimation;
38+
3339
/// Whether the content is expanded.
3440
bool expand = false;
3541

42+
@override
43+
void initState() {
44+
super.initState();
45+
expandController = AnimationController(vsync: this, duration: Duration(milliseconds: 200));
46+
expandAnimation = CurvedAnimation(
47+
parent: expandController,
48+
curve: Curves.fastOutSlowIn,
49+
);
50+
}
51+
3652
@override
3753
Widget build(BuildContext context) => Column(
3854
mainAxisSize: MainAxisSize.min,
@@ -41,7 +57,13 @@ class _ExpandListTileState extends State<ExpandListTile> with BrightnessListener
4157
iconColor: Theme.of(context).colorScheme.primary,
4258
title: widget.title,
4359
onTap: () {
44-
setState(() => expand = !expand);
60+
bool willExpand = !expand;
61+
setState(() => expand = willExpand);
62+
if (willExpand) {
63+
expandController.forward();
64+
} else {
65+
expandController.reverse();
66+
}
4567
},
4668
trailing: AnimatedRotation(
4769
turns: expand ? 0.25 : 0,
@@ -53,7 +75,26 @@ class _ExpandListTileState extends State<ExpandListTile> with BrightnessListener
5375
),
5476
enabled: widget.enabled,
5577
),
56-
if (expand) ...widget.children,
78+
SizeTransition(
79+
axisAlignment: 1.0,
80+
sizeFactor: expandAnimation,
81+
child: Column(
82+
mainAxisSize: MainAxisSize.min,
83+
children: widget.children,
84+
),
85+
),
86+
// for (Widget child in widget.children)
87+
// SizeTransition(
88+
// axisAlignment: 1.0,
89+
// sizeFactor: expandAnimation,
90+
// child: child,
91+
// ),
5792
],
5893
);
94+
95+
@override
96+
void dispose() {
97+
expandController.dispose();
98+
super.dispose();
99+
}
59100
}

0 commit comments

Comments
 (0)