@@ -19,6 +19,7 @@ class GFToast extends StatefulWidget {
1919 this .animationDuration = const Duration (milliseconds: 300 ),
2020 this .duration = const Duration (milliseconds: 300 ),
2121 this .textStyle = const TextStyle (color: Colors .white70),
22+ this . autoDismissDuration= const Duration (milliseconds: 3000 ),
2223 }) : super (key: key);
2324
2425 /// child of type [Widget] is alternative to text key. text will get priority over child
@@ -51,6 +52,9 @@ class GFToast extends StatefulWidget {
5152 ///type of [Duration] which takes the duration of the animation
5253 final Duration duration;
5354
55+ ///type of [Duration] which takes the duration of the autoDismiss
56+ final Duration autoDismissDuration;
57+
5458 /// type of [Alignment] used to align the text inside the toast
5559 final Alignment alignment;
5660
@@ -74,37 +78,43 @@ class _GFToastState extends State<GFToast> with TickerProviderStateMixin {
7478 curve: Curves .easeIn,
7579 );
7680
77- animationController.forward ();
78- fadeanimationController = AnimationController (
79- vsync: this ,
80- duration: widget.animationDuration,
81- )..addListener (() => setState (() {}));
82- fadeanimation = Tween <double >(
83- begin: 0 ,
84- end: 1 ,
85- ).animate (fadeanimationController);
86- Timer (widget.duration, () {
87- fadeanimationController.forward ();
88- });
89-
90- fadeanimation = Tween <double >(
91- begin: 1 ,
92- end: 0 ,
93- ).animate (fadeanimationController);
94- fadeanimation.addStatusListener ((AnimationStatus state) {
95- if (fadeanimation.isCompleted && widget.autoDismiss) {
96- setState (() {
97- hideToast = true ;
98- });
99- }
100- });
81+ if (mounted) {
82+ animationController.forward ();
83+ fadeanimationController = AnimationController (
84+ vsync: this ,
85+ duration: widget.animationDuration,
86+ )
87+ ..addListener (() => setState (() {}));
88+ fadeanimation = Tween <double >(
89+ begin: 0 ,
90+ end: 1 ,
91+ ).animate (fadeanimationController);
92+ Timer (widget.duration, () {
93+ if (mounted){
94+ fadeanimationController.forward ();
95+ }
96+ });
97+ fadeanimation = Tween <double >(
98+ begin: 1 ,
99+ end: 0 ,
100+ ).animate (fadeanimationController);
101+ fadeanimation.addStatusListener ((AnimationStatus state) {
102+ if (fadeanimation.isCompleted && widget.autoDismiss) {
103+ setState (() {
104+ hideToast = true ;
105+ });
106+ }
107+ });
108+ }
109+
101110 super .initState ();
102111 }
103112
104113 @override
105114 void dispose () {
106115 animationController.dispose ();
107116 fadeanimationController.dispose ();
117+
108118 super .dispose ();
109119 }
110120
0 commit comments