@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
33import 'package:flutter/widgets.dart' ;
44import 'package:getflutter/colors/gf_color.dart' ;
55import 'package:getflutter/components/toast/gf_floating_widget.dart' ;
6+ import 'package:getflutter/types/gf_toast_type.dart' ;
67
78class GFToast extends StatefulWidget {
89 ///Creates [GFToast] that can be used to display quick warning or error messages.
@@ -14,6 +15,11 @@ class GFToast extends StatefulWidget {
1415 this .backgroundColor,
1516 this .text,
1617 this .width,
18+ this .type = GFToastType .basic,
19+ this .autoDismiss = true ,
20+ this .alignment,
21+ this .animationDuration = const Duration (seconds: 2 ),
22+ this .duration = const Duration (seconds: 2 ),
1723 this .textStyle = const TextStyle (color: Colors .white70),
1824 }) : super (key: key);
1925
@@ -32,86 +38,137 @@ class GFToast extends StatefulWidget {
3238 /// textStyle of type [textStyle] will be applicable to text only and not for the child
3339 final TextStyle textStyle;
3440
35- /// width od type [double] used to control the width od the [GFToast]
41+ /// width of type [double] used to control the width of the [GFToast]
3642 final double width;
3743
44+ ///type of [GFToastType] which takes the type ie, basic, rounded and fullWidth for the [GFToast]
45+ final GFToastType type;
46+
47+ ///type of [bool] which takes bool values ie, true or false to automatically hide the [GFToast] message
48+ final bool autoDismiss;
49+
50+ ///type of [Duration] which takes the duration of the fade in animation
51+ final Duration animationDuration;
52+
53+ ///type of [Duration] which takes the duration of the animation
54+ final Duration duration;
55+
56+ /// type of [Alignment] used to align the text inside the toast
57+ final Alignment alignment;
58+
3859 @override
3960 _GFToastState createState () => _GFToastState ();
4061}
4162
4263class _GFToastState extends State <GFToast > with TickerProviderStateMixin {
43- AnimationController controller, _controller;
44- Animation <Offset > offset, offset1;
45- Animation <double > animation;
46- Timer timer;
47-
48- bool slide = false ;
64+ AnimationController animationController, fadeanimationController;
65+ Animation <double > animation, fadeanimation;
66+ bool hideToast = false ;
4967
5068 @override
5169 void initState () {
52- super .initState ();
70+ animationController = AnimationController (
71+ duration: const Duration (milliseconds: 2000 ), vsync: this );
72+ animation =
73+ CurvedAnimation (parent: animationController, curve: Curves .easeIn);
5374
54- controller = AnimationController (
55- duration: const Duration (milliseconds: 300 ), vsync: this );
56- animation = CurvedAnimation (parent: controller, curve: Curves .easeIn);
57- _controller =
58- AnimationController (vsync: this , duration: Duration (milliseconds: 200 ));
59- offset = Tween <Offset >(begin: Offset .zero, end: Offset (0.0 , 1.0 ))
60- .animate (_controller);
61- controller.forward ();
62- _controller.forward ();
75+ animationController.forward ();
76+
77+ fadeanimationController = AnimationController (
78+ vsync: this ,
79+ duration: widget.animationDuration,
80+ )..addListener (() => setState (() {}));
81+ fadeanimation = Tween <double >(
82+ begin: 0.0 ,
83+ end: 1.0 ,
84+ ).animate (fadeanimationController);
85+ Timer (widget.duration, () {
86+ fadeanimationController.forward ();
87+ });
88+
89+ fadeanimation = Tween <double >(
90+ begin: 1.0 ,
91+ end: 0.0 ,
92+ ).animate (fadeanimationController);
93+
94+ fadeanimation.addStatusListener ((AnimationStatus state) {
95+ if (fadeanimation.isCompleted && widget.autoDismiss) {
96+ setState (() {
97+ hideToast = true ;
98+ });
99+ }
100+ });
101+ super .initState ();
63102 }
64103
65104 @override
66105 void dispose () {
67- controller.dispose ();
106+ animationController.dispose ();
107+ fadeanimationController.dispose ();
68108 super .dispose ();
69109 }
70110
71111 @override
72112 Widget build (BuildContext context) {
73- return FadeTransition (
74- opacity: animation,
75- child: Column (
76- children: < Widget > [
77- Container (
78- width: widget.width != null ? widget.width : null ,
79- constraints: BoxConstraints (minHeight: 50.0 ),
80- // width: 100,
81- margin: EdgeInsets .only (left: 10 , right: 10 ),
82- padding: EdgeInsets .all (10 ),
83- decoration: BoxDecoration (
84- borderRadius: BorderRadius .all (Radius .circular (3 )),
85- color: widget.backgroundColor != null
86- ? GFColors .getGFColor (widget.backgroundColor)
87- : Color (0xff323232 ),
88- ),
89- child: Row (
113+ return hideToast
114+ ? Container ()
115+ : FadeTransition (
116+ opacity: widget.autoDismiss ? fadeanimation : animation,
117+ child: Column (
90118 children: < Widget > [
91- Flexible (
92- flex: 7 ,
93- fit: FlexFit .tight,
94- child: widget.text != null
95- ? Text (widget.text, style: widget.textStyle)
96- : (widget.child ?? Container ()),
97- ),
98- SizedBox (
99- width: 10 ,
119+ Container (
120+ width: widget.type == GFToastType .fullWidth
121+ ? MediaQuery .of (context).size.width
122+ : widget.width,
123+ constraints: BoxConstraints (minHeight: 50.0 ),
124+ margin: widget.type == GFToastType .fullWidth
125+ ? EdgeInsets .only (left: 0 , right: 0 )
126+ : EdgeInsets .only (left: 10 , right: 10 ),
127+ padding: EdgeInsets .all (10 ),
128+ decoration: BoxDecoration (
129+ borderRadius: widget.type == GFToastType .basic
130+ ? BorderRadius .circular (0.0 )
131+ : widget.type == GFToastType .rounded
132+ ? BorderRadius .circular (10.0 )
133+ : BorderRadius .zero,
134+ color: widget.backgroundColor != null
135+ ? GFColors .getGFColor (widget.backgroundColor)
136+ : Color (0xff323232 ),
137+ boxShadow: [
138+ BoxShadow (
139+ color: Colors .black.withOpacity (0.40 ),
140+ blurRadius: 6.0 )
141+ ]),
142+ child: Row (
143+ children: < Widget > [
144+ Flexible (
145+ flex: 7 ,
146+ fit: FlexFit .tight,
147+ child: Align (
148+ alignment: widget.alignment != null
149+ ? widget.alignment
150+ : Alignment .topLeft,
151+ child: widget.text != null
152+ ? Text (widget.text, style: widget.textStyle)
153+ : (widget.child ?? Container ()),
154+ )),
155+ SizedBox (
156+ width: 10 ,
157+ ),
158+ widget.button != null
159+ ? Flexible (
160+ flex: 4 ,
161+ fit: FlexFit .tight,
162+ child: Align (
163+ alignment: Alignment .topRight,
164+ child: widget.button,
165+ ))
166+ : Container ()
167+ ],
168+ ),
100169 ),
101- widget.button != null
102- ? Flexible (
103- flex: 4 ,
104- fit: FlexFit .tight,
105- child: Align (
106- alignment: Alignment .topRight,
107- child: widget.button,
108- ))
109- : Container ()
110170 ],
111171 ),
112- ),
113- ],
114- ),
115- );
172+ );
116173 }
117174}
0 commit comments