-
Notifications
You must be signed in to change notification settings - Fork 784
/
fade_in.dart
118 lines (104 loc) · 2.71 KB
/
fade_in.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
import 'package:flutter/material.dart';
enum FadeDirection {
startToEnd,
endToStart,
topToBottom,
bottomToTop,
}
class FadeIn extends StatefulWidget {
const FadeIn({
super.key,
this.fadeDirection = FadeDirection.startToEnd,
this.offset = 1.0,
this.controller,
this.duration,
this.curve = Curves.easeOut,
required this.child,
}) : assert(
controller == null && duration != null ||
controller != null && duration == null,
),
assert(offset > 0);
/// [FadeIn] animation can be controlled via external [controller]. If
/// [controller] is not provided, it will use the default internal controller
/// which will run the animation in initState()
final AnimationController? controller;
final FadeDirection fadeDirection;
final double offset;
final Widget child;
final Duration? duration;
final Curve curve;
@override
_FadeInState createState() => _FadeInState();
}
class _FadeInState extends State<FadeIn> with SingleTickerProviderStateMixin {
AnimationController? _controller;
late Animation<Offset> _slideAnimation;
late Animation<double> _opacityAnimation;
@override
void initState() {
super.initState();
if (widget.controller == null) {
_controller = AnimationController(
vsync: this,
duration: widget.duration,
);
}
_updateAnimations();
_controller?.forward();
}
void _updateAnimations() {
Offset? begin;
Offset? end;
final offset = widget.offset;
switch (widget.fadeDirection) {
case FadeDirection.startToEnd:
begin = Offset(-offset, 0);
end = Offset.zero;
case FadeDirection.endToStart:
begin = Offset(offset, 0);
end = Offset.zero;
case FadeDirection.topToBottom:
begin = Offset(0, -offset);
end = Offset.zero;
case FadeDirection.bottomToTop:
begin = Offset(0, offset);
end = Offset.zero;
}
_slideAnimation = Tween<Offset>(
begin: begin,
end: end,
).animate(
CurvedAnimation(
parent: _effectiveController!,
curve: widget.curve,
),
);
_opacityAnimation = Tween<double>(
begin: 0.0,
end: 1.0,
).animate(
CurvedAnimation(
parent: _effectiveController!,
curve: widget.curve,
),
);
}
AnimationController? get _effectiveController =>
widget.controller ?? _controller;
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SlideTransition(
position: _slideAnimation,
child: FadeTransition(
opacity: _opacityAnimation,
child: widget.child,
),
);
}
}