-
Notifications
You must be signed in to change notification settings - Fork 1
/
typewriter_text_animation.dart
97 lines (84 loc) · 2.25 KB
/
typewriter_text_animation.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
import 'package:flutter/material.dart';
class TypewriterText extends StatefulWidget {
final String text;
final TextStyle textStyle;
final Duration duration;
const TypewriterText({super.key,
required this.text,
required this.textStyle,
required this.duration,
});
@override
// ignore: library_private_types_in_public_api
_TypewriterTextState createState() => _TypewriterTextState();
}
class _TypewriterTextState extends State<TypewriterText> with TickerProviderStateMixin {
late AnimationController _controller;
late List<Animation<double>> _letterAnimations;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: widget.duration,
);
_letterAnimations = List.generate(
widget.text.length,
(index) => Tween<double>(
begin: 0.0,
end: 1.0,
).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(
index / widget.text.length,
(index + 1) / widget.text.length,
curve: Curves.easeInOut,
),
),
),
);
_controller.forward();
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: List.generate(
widget.text.length,
(index) => FadeTransition(
opacity: _letterAnimations[index],
child: Text(
widget.text[index],
style: widget.textStyle,
),
),
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
class TypewriterTextAnimationScreen extends StatelessWidget {
const TypewriterTextAnimationScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
backgroundColor: Colors.black,
body: Center(
child: TypewriterText(
text: 'Hello, World!',
textStyle: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold,
color: Colors.pink
),
duration: Duration(seconds: 5),
),
),
);
}
}