/
example_custom_section_animation.dart
143 lines (126 loc) · 4 KB
/
example_custom_section_animation.dart
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import 'package:example/widgets.dart';
import 'package:flutter/material.dart';
import 'package:sticky_and_expandable_list/sticky_and_expandable_list.dart';
import 'mock_data.dart';
class ExampleCustomSectionAnimation extends StatefulWidget {
@override
_ExampleCustomSectionAnimationState createState() =>
_ExampleCustomSectionAnimationState();
}
class _ExampleCustomSectionAnimationState
extends State<ExampleCustomSectionAnimation> {
var sectionList = MockData.getExampleSections(3, 3);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(title: "CustomSectionAnimation Example"),
body: ExpandableListView(
builder: SliverExpandableChildDelegate<String, ExampleSection>(
sectionList: sectionList,
itemBuilder: (context, sectionIndex, itemIndex, index) {
String item = sectionList[sectionIndex].items[itemIndex];
return ListTile(
leading: CircleAvatar(
child: Text("$index"),
),
title: Text(item),
);
},
sectionBuilder: (context, containerInfo) => _SectionWidget(
section: sectionList[containerInfo.sectionIndex],
containerInfo: containerInfo,
onStateChanged: () {
//notify ExpandableListView that expand state has changed.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
setState(() {});
}
});
},
),
),
));
}
}
class _SectionWidget extends StatefulWidget {
final ExampleSection section;
final ExpandableSectionContainerInfo containerInfo;
final VoidCallback onStateChanged;
_SectionWidget(
{required this.section,
required this.containerInfo,
required this.onStateChanged});
@override
__SectionWidgetState createState() => __SectionWidgetState();
}
class __SectionWidgetState extends State<_SectionWidget>
with SingleTickerProviderStateMixin {
static final Animatable<double> _halfTween =
Tween<double>(begin: 0.0, end: 0.5);
late AnimationController _controller;
late Animation _iconTurns;
late Animation<double> _heightFactor;
@override
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: Duration(milliseconds: 300));
_iconTurns =
_controller.drive(_halfTween.chain(CurveTween(curve: Curves.easeIn)));
_heightFactor = _controller.drive(CurveTween(curve: Curves.easeIn));
if (widget.section.isSectionExpanded()) {
_controller.value = 1;
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
widget.containerInfo
..header = _buildHeader(context)
..content = _buildContent(context);
return ExpandableSectionContainer(
info: widget.containerInfo,
);
}
Widget _buildHeader(BuildContext context) {
return Container(
color: Colors.lightBlue,
child: ListTile(
title: Text(
widget.section.header,
style: TextStyle(color: Colors.white),
),
trailing: RotationTransition(
turns: _iconTurns as Animation<double>,
child: const Icon(
Icons.expand_more,
color: Colors.white70,
),
),
onTap: _onTap,
),
);
}
void _onTap() {
widget.section.setSectionExpanded(!widget.section.isSectionExpanded());
if (widget.section.isSectionExpanded()) {
widget.onStateChanged();
_controller.forward();
} else {
_controller.reverse().then((_) {
widget.onStateChanged();
});
}
}
Widget _buildContent(BuildContext context) {
return SizeTransition(
sizeFactor: _heightFactor,
child: SliverExpandableChildDelegate.buildDefaultContent(
context, widget.containerInfo),
);
}
}