Skip to content

Commit c30f082

Browse files
committed
added fade in and out animation in gf-toast
1 parent 554c8b0 commit c30f082

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

lib/components/toast/gf_toast.dart

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class GFToast extends StatefulWidget {
1717
this.width,
1818
this.type = GFToastType.basic,
1919
this.autoDismiss = true,
20+
this.alignment,
21+
this.animationDuration = const Duration(seconds: 2),
2022
this.duration = const Duration(seconds: 2),
2123
this.textStyle = const TextStyle(color: Colors.white70),
2224
}) : super(key: key);
@@ -46,38 +48,56 @@ class GFToast extends StatefulWidget {
4648
final bool autoDismiss;
4749

4850
///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
4954
final Duration duration;
5055

56+
/// type of [Alignment] used to align the text inside the toast
57+
final Alignment alignment;
58+
5159
@override
5260
_GFToastState createState() => _GFToastState();
5361
}
5462

5563
class _GFToastState extends State<GFToast> with TickerProviderStateMixin {
5664
AnimationController animationController, fadeanimationController;
5765
Animation<double> animation, fadeanimation;
66+
bool hideToast = false;
5867

5968
@override
6069
void initState() {
6170
animationController = AnimationController(
62-
duration: const Duration(milliseconds: 300), vsync: this);
71+
duration: const Duration(milliseconds: 2000), vsync: this);
6372
animation =
6473
CurvedAnimation(parent: animationController, curve: Curves.easeIn);
6574

6675
animationController.forward();
6776

6877
fadeanimationController = AnimationController(
6978
vsync: this,
70-
duration: widget.duration,
79+
duration: widget.animationDuration,
7180
)..addListener(() => setState(() {}));
7281
fadeanimation = Tween<double>(
7382
begin: 0.0,
7483
end: 1.0,
7584
).animate(fadeanimationController);
76-
fadeanimationController.forward();
85+
Timer(widget.duration,(){
86+
fadeanimationController.forward();
87+
});
88+
7789
fadeanimation = Tween<double>(
7890
begin: 1.0,
7991
end: 0.0,
8092
).animate(fadeanimationController);
93+
94+
fadeanimation.addStatusListener((AnimationStatus state){
95+
if(fadeanimation.isCompleted && widget.autoDismiss){
96+
setState(() {
97+
hideToast = true;
98+
});
99+
}
100+
});
81101
super.initState();
82102
}
83103

@@ -90,7 +110,7 @@ class _GFToastState extends State<GFToast> with TickerProviderStateMixin {
90110

91111
@override
92112
Widget build(BuildContext context) {
93-
return FadeTransition(
113+
return hideToast?Container(): FadeTransition(
94114
opacity: widget.autoDismiss ? fadeanimation : animation,
95115
child: Column(
96116
children: <Widget>[
@@ -119,9 +139,12 @@ class _GFToastState extends State<GFToast> with TickerProviderStateMixin {
119139
Flexible(
120140
flex: 7,
121141
fit: FlexFit.tight,
122-
child: widget.text != null
123-
? Text(widget.text, style: widget.textStyle)
124-
: (widget.child ?? Container()),
142+
child: Align(
143+
alignment: widget.alignment !=null ? widget.alignment: Alignment.center,
144+
child: widget.text != null
145+
? Text(widget.text, style: widget.textStyle)
146+
: (widget.child ?? Container()),
147+
)
125148
),
126149
SizedBox(
127150
width: 10,

0 commit comments

Comments
 (0)