@@ -18,9 +18,16 @@ class GFAccordion extends StatefulWidget {
1818 this .contentChild,
1919 this .titleborder = const Border (),
2020 this .contentBorder = const Border (),
21- this .margin})
21+ this .margin,
22+ this .showAccordion = false ,
23+ this .onToggleCollapsed})
2224 : super (key: key);
2325
26+ final Function (bool ) onToggleCollapsed;
27+
28+ /// controls if the accordion should be collapsed or not making it possible to be controlled from outside
29+ final bool showAccordion;
30+
2431 /// child of type [Widget] is alternative to title key. title will get priority over titleChild
2532 final Widget titleChild;
2633
@@ -75,10 +82,11 @@ class _GFAccordionState extends State<GFAccordion>
7582 AnimationController animationController;
7683 AnimationController controller;
7784 Animation <Offset > offset;
78- bool showAccordion = false ;
85+ bool showAccordion;
7986
8087 @override
8188 void initState () {
89+ showAccordion = widget.showAccordion;
8290 animationController = AnimationController (
8391 duration: const Duration (seconds: 2 ),
8492 vsync: this ,
@@ -114,18 +122,7 @@ class _GFAccordionState extends State<GFAccordion>
114122 children: < Widget > [
115123 GestureDetector (
116124 onTap: () {
117- setState (() {
118- switch (controller.status) {
119- case AnimationStatus .completed:
120- controller.forward (from: 0 );
121- break ;
122- case AnimationStatus .dismissed:
123- controller.forward ();
124- break ;
125- default :
126- }
127- showAccordion = ! showAccordion;
128- });
125+ _toggleCollapsed ();
129126 },
130127 child: Container (
131128 decoration: BoxDecoration (
@@ -166,4 +163,22 @@ class _GFAccordionState extends State<GFAccordion>
166163 ],
167164 ),
168165 );
166+
167+ void _toggleCollapsed () {
168+ setState (() {
169+ switch (controller.status) {
170+ case AnimationStatus .completed:
171+ controller.forward (from: 0 );
172+ break ;
173+ case AnimationStatus .dismissed:
174+ controller.forward ();
175+ break ;
176+ default :
177+ }
178+ showAccordion = ! showAccordion;
179+ if (widget.onToggleCollapsed != null ) {
180+ widget.onToggleCollapsed (showAccordion);
181+ }
182+ });
183+ }
169184}
0 commit comments