-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathrotate.dart
203 lines (184 loc) · 6.56 KB
/
rotate.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import 'package:flutter/material.dart';
import 'animated_text.dart';
/// Animated Text that rotates a [Text] in and then out.
///
/// 
class RotateAnimatedText extends AnimatedText {
/// Transition height.
///
/// By default it is set to [TextStyle.fontSize] * 10 / 3.
final double? transitionHeight;
/// Adds [AlignmentGeometry] property to the text in the widget.
///
/// By default it is set to [Alignment.center]
final AlignmentGeometry alignment;
/// Specifies the [TextDirection] for resolving alignment.
///
/// By default it is set to [TextDirection.ltr]
final TextDirection textDirection;
/// Controls whether the text:
/// * rotates in _and_ out (true), or
/// * just rotates _in_ (false).
///
/// Note that you may want to adjust the [duration] when mixing
/// [RotateAnimatedText] instances with mixed [rotateOut] values.
///
/// By default, it is set to true.
final bool rotateOut;
/// The Duration of text hold, default value = const Duration(milliseconds: 2000)
/// Total Duration = animationDuration * (rotateOut ? 2 : 1) + holdDuration
final Duration holdDuration;
/// The Duration text will take to animate in and out.
/// By default it is set to const Duration(milliseconds: 1000)
/// Out Duration will only be applied if [rotateOut] is set to `true`
/// Total Duration = animationDuration * (rotateOut ? 2 : 1) + holdDuration
final Duration animationDuration;
RotateAnimatedText(
String text, {
TextAlign textAlign = TextAlign.start,
TextStyle? textStyle,
this.animationDuration = const Duration(milliseconds: 1000),
this.holdDuration = const Duration(microseconds: 2000),
this.transitionHeight,
this.alignment = Alignment.center,
this.textDirection = TextDirection.ltr,
this.rotateOut = true,
}) : super(
text: text,
textAlign: textAlign,
textStyle: textStyle,
duration: animationDuration * (rotateOut ? 2 : 1) + holdDuration,
);
late Animation<double> _fadeIn, _fadeOut;
late Animation<Alignment> _slideIn, _slideOut;
@override
void initAnimation(AnimationController controller) {
final direction = textDirection;
final inIntervalEndTime = animationDuration.inMilliseconds;
final outIntervalStartTime =
inIntervalEndTime + holdDuration.inMilliseconds;
_slideIn = AlignmentTween(
begin: Alignment.topCenter.add(alignment).resolve(direction),
end: Alignment.center.add(alignment).resolve(direction),
).animate(
CurvedAnimation(
parent: controller,
curve: Interval(0.0, inIntervalEndTime / duration.inMilliseconds,
curve: Curves.linear),
),
);
_fadeIn = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: controller,
curve: Interval(0.0, inIntervalEndTime / duration.inMilliseconds,
curve: Curves.easeOut),
),
);
if (rotateOut) {
_slideOut = AlignmentTween(
begin: Alignment.center.add(alignment).resolve(direction),
end: Alignment.bottomCenter.add(alignment).resolve(direction),
).animate(
CurvedAnimation(
parent: controller,
curve: Interval(outIntervalStartTime / duration.inMilliseconds, 1.0,
curve: Curves.linear),
),
);
_fadeOut = Tween<double>(begin: 1.0, end: 0.0).animate(
CurvedAnimation(
parent: controller,
curve: Interval(outIntervalStartTime / duration.inMilliseconds, 1.0,
curve: Curves.easeIn),
),
);
}
}
@override
Widget completeText(BuildContext context) =>
rotateOut ? SizedBox.shrink() : super.completeText(context);
@override
Widget animatedBuilder(BuildContext context, Widget? child) {
final fontSize =
textStyle?.fontSize ?? DefaultTextStyle.of(context).style.fontSize;
return SizedBox(
height: transitionHeight ?? (fontSize! * 10 / 3),
child: AlignTransition(
alignment: _slideIn.value.y != 0.0 || !rotateOut ? _slideIn : _slideOut,
child: Opacity(
opacity: _fadeIn.value != 1.0 || !rotateOut
? _fadeIn.value
: _fadeOut.value,
child: textWidget(text),
),
),
);
}
}
/// Animation that displays [text] elements, rotating them in one at a time.
///
/// 
@Deprecated('Use AnimatedTextKit with RotateAnimatedText instead.')
class RotateAnimatedTextKit extends AnimatedTextKit {
RotateAnimatedTextKit({
Key? key,
required List<String> text,
TextAlign textAlign = TextAlign.start,
TextStyle? textStyle,
double? transitionHeight,
AlignmentGeometry alignment = Alignment.center,
TextDirection textDirection = TextDirection.ltr,
Duration duration = const Duration(milliseconds: 2000),
Duration pause = const Duration(milliseconds: 500),
VoidCallback? onTap,
void Function(int, bool)? onNext,
void Function(int, bool)? onNextBeforePause,
VoidCallback? onFinished,
bool isRepeatingAnimation = true,
int totalRepeatCount = 3,
bool repeatForever = false,
bool displayFullTextOnTap = false,
bool stopPauseOnTap = false,
}) : super(
key: key,
animatedTexts: _animatedTexts(
text,
textAlign,
textStyle,
duration,
transitionHeight,
alignment,
textDirection,
),
pause: pause,
displayFullTextOnTap: displayFullTextOnTap,
stopPauseOnTap: stopPauseOnTap,
onTap: onTap,
onNext: onNext,
onNextBeforePause: onNextBeforePause,
onFinished: onFinished,
isRepeatingAnimation: isRepeatingAnimation,
totalRepeatCount: totalRepeatCount,
repeatForever: repeatForever,
);
static List<AnimatedText> _animatedTexts(
List<String> text,
TextAlign textAlign,
TextStyle? textStyle,
Duration duration,
double? transitionHeight,
AlignmentGeometry alignment,
TextDirection textDirection,
) =>
text
.map((_) => RotateAnimatedText(
_,
textAlign: textAlign,
textStyle: textStyle,
animationDuration: duration,
transitionHeight: transitionHeight,
alignment: alignment,
textDirection: textDirection,
))
.toList();
}