You can modernise your users' experience when creating Flutter apps by injecting cool animations with regards to text. This tutorial will be an exploration of some of the solutions and packages, and how to use them to animate texts in our apps.
(a). Use Animated Text Kit
Animated Text Kit is a flutter package project which contains a collection of cool and beautiful text animations.
Here is a demo:
Here's how to use it.
Step 1: Install it
Start by installing the package. Do this by declaring the package as a flutter dependency in your pubspec.yaml
:
dependencies:
animated_text_kit: ^4.2.1
Step 2: Import it
After installation you need toimport the library into your Dart code. You do that by adding the following line at the top of the file:
import 'package:animated_text_kit/animated_text_kit.dart';
Step 3: Animate Text
After import you can proceed and animate your text content. Here is a sample code that animates Hello World
Text, using the Typewiter Animation
.
AnimatedTextKit(
animatedTexts: [
TypewriterAnimatedText(
'Hello world!',
textStyle: const TextStyle(
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
speed: const Duration(milliseconds: 2000),
),
],
totalRepeatCount: 4,
pause: const Duration(milliseconds: 1000),
displayFullTextOnTap: true,
stopPauseOnTap: true,
)
The parameters include:
pause
– the time of the pause between animation textsdisplayFullTextOnTap
– tapping the animation will rush it to completionisRepeatingAnimation
– controls whether the animation repeatsrepeatForever
– controls whether the animation repeats forevertotalRepeatCount
– number of times the animation should repeat (whenrepeatForever
isfalse
)
HowTo Text Animation Examples
Here are snippets of how to animate texts in various ways using this package:
How to Rotate text
Here is the code for rotating Text:
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const SizedBox(width: 20.0, height: 100.0),
const Text(
'Be',
style: TextStyle(fontSize: 43.0),
),
const SizedBox(width: 20.0, height: 100.0),
DefaultTextStyle(
style: const TextStyle(
fontSize: 40.0,
fontFamily: 'Horizon',
),
child: AnimatedTextKit(
animatedTexts: [
RotateAnimatedText('AWESOME'),
RotateAnimatedText('OPTIMISTIC'),
RotateAnimatedText('DIFFERENT'),
]
onTap: () {
print("Tap Event");
},
),
),
],
);
How to Fade text
You can also fade text using this library. Here is the code to do so:
return SizedBox(
width: 250.0,
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
child: AnimatedTextKit(
animatedTexts: [
FadeAnimatedText('do IT!'),
FadeAnimatedText('do it RIGHT!!'),
FadeAnimatedText('do it RIGHT NOW!!!'),
],
onTap: () {
print("Tap Event");
},
),
),
);
Type Text Animation
You can also create typer text animation using the following code:
return SizedBox(
width: 250.0,
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 30.0,
fontFamily: 'Bobbers',
),
child: AnimatedTextKit(
animatedTexts: [
TyperAnimatedText('It is not enough to do your best,'),
TyperAnimatedText('you must know what to do,'),
TyperAnimatedText('and then do your best'),
TyperAnimatedText('- W.Edwards Deming'),
]
onTap: () {
print("Tap Event");
},
),
),
);
TypeWriter Text Animation
You can create typewriter text animation too:
return SizedBox(
width: 250.0,
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 30.0,
fontFamily: 'Agne',
),
child: AnimatedTextKit(
animatedTexts: [
TypewriterAnimatedText('Discipline is the best tool'),
TypewriterAnimatedText('Design first, then code'),
TypewriterAnimatedText('Do not patch bugs out, rewrite them'),
TypewriterAnimatedText('Do not test bugs out, design them out'),
],
onTap: () {
print("Tap Event");
},
),
),
);
Text Scale Animation
You can also animate text by scaling it. You do that as follows:
return SizedBox(
width: 250.0,
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 70.0,
fontFamily: 'Canterbury',
),
child: AnimatedTextKit(
animatedTexts: [
ScaleAnimatedText('Think'),
ScaleAnimatedText('Build'),
ScaleAnimatedText('Ship'),
],
onTap: () {
print("Tap Event");
},
),
),
);
Text Coloring animation
You can also animate text by coloring it:
const colorizeColors = [
Colors.purple,
Colors.blue,
Colors.yellow,
Colors.red,
];
const colorizeTextStyle = TextStyle(
fontSize: 50.0,
fontFamily: 'Horizon',
);
return SizedBox(
width: 250.0,
child: AnimatedTextKit(
animatedTexts: [
ColorizeAnimatedText(
'Larry Page',
textStyle: colorizeTextStyle,
colors: colorizeColors,
),
ColorizeAnimatedText(
'Bill Gates',
textStyle: colorizeTextStyle,
colors: colorizeColors,
),
ColorizeAnimatedText(
'Steve Jobs',
textStyle: colorizeTextStyle,
colors: colorizeColors,
),
],
isRepeatingAnimation: true,
onTap: () {
print("Tap Event");
},
),
);
Text Liquid Fill
Here is another modern animation, simulating liquid filling text:
return SizedBox(
width: 250.0,
child: TextLiquidFill(
text: 'LIQUIDY',
waveColor: Colors.blueAccent,
boxBackgroundColor: Colors.redAccent,
textStyle: TextStyle(
fontSize: 80.0,
fontWeight: FontWeight.bold,
),
boxHeight: 300.0,
),
);
Wavy Text Animation
Another commonly used animation type is waving of Text:
return DefaultTextStyle(
style: const TextStyle(
fontSize: 20.0,
),
child: AnimatedTextKit(
animatedTexts: [
WavyAnimatedText('Hello World'),
WavyAnimatedText('Look at the waves'),
],
isRepeatingAnimation: true,
onTap: () {
print("Tap Event");
},
),
);
Text Flicker Animation
You can also animate text by flickering. Here is how to do that:
return SizedBox(
width: 250.0,
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 35,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 7.0,
color: Colors.white,
offset: Offset(0, 0),
),
],
),
child: AnimatedTextKit(
repeatForever: true,
animatedTexts: [
FlickerAnimatedText('Flicker Frenzy'),
FlickerAnimatedText('Night Vibes On'),
FlickerAnimatedText("C'est La Vie !"),
],
onTap: () {
print("Tap Event");
},
),
),
);
Full Example
Below is a ful example.
Start by installing the package as has been described above.
Then replace your main.dart
with the following code:
main.dart
import 'package:flutter/material.dart';
import 'package:animated_text_kit/animated_text_kit.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
/// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Animated Text Kit',
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late List<AnimatedTextExample> _examples;
int _index = 0;
int _tapCount = 0;
@override
void initState() {
super.initState();
_examples = animatedTextExamples(onTap: () {
print('Tap Event');
setState(() {
_tapCount++;
});
});
}
@override
Widget build(BuildContext context) {
final animatedTextExample = _examples[_index];
return Scaffold(
appBar: AppBar(
title: Text(
animatedTextExample.label,
style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
),
),
body: Column(
children: <Widget>[
Expanded(
child: Container(),
),
Container(
decoration: BoxDecoration(color: animatedTextExample.color),
height: 300.0,
width: 300.0,
child: Center(
key: ValueKey(animatedTextExample.label),
child: animatedTextExample.child,
),
),
Expanded(
child: Container(
alignment: Alignment.center,
child: Text('Taps: $_tapCount'),
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_index = ++_index % _examples.length;
_tapCount = 0;
});
},
tooltip: 'Next',
child: const Icon(
Icons.play_circle_filled,
size: 50.0,
),
),
);
}
}
class AnimatedTextExample {
final String label;
final Color? color;
final Widget child;
const AnimatedTextExample({
required this.label,
required this.color,
required this.child,
});
}
// Colorize Text Style
const _colorizeTextStyle = TextStyle(
fontSize: 50.0,
fontFamily: 'Horizon',
);
// Colorize Colors
const _colorizeColors = [
Colors.purple,
Colors.blue,
Colors.yellow,
Colors.red,
];
List<AnimatedTextExample> animatedTextExamples({VoidCallback? onTap}) =>
<AnimatedTextExample>[
AnimatedTextExample(
label: 'Rotate',
color: Colors.orange[800],
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const SizedBox(
width: 20.0,
height: 100.0,
),
const Text(
'Be',
style: TextStyle(fontSize: 43.0),
),
const SizedBox(
width: 20.0,
height: 100.0,
),
DefaultTextStyle(
style: TextStyle(
fontSize: 40.0,
fontFamily: 'Horizon',
),
child: AnimatedTextKit(
animatedTexts: [
RotateAnimatedText('AWESOME'),
RotateAnimatedText('OPTIMISTIC'),
RotateAnimatedText(
'DIFFERENT',
textStyle: const TextStyle(
decoration: TextDecoration.underline,
),
),
],
onTap: onTap,
isRepeatingAnimation: true,
totalRepeatCount: 10,
),
),
],
),
],
),
),
AnimatedTextExample(
label: 'Fade',
color: Colors.brown[600],
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
child: AnimatedTextKit(
animatedTexts: [
FadeAnimatedText('do IT!'),
FadeAnimatedText('do it RIGHT!!'),
FadeAnimatedText('do it RIGHT NOW!!!'),
],
onTap: onTap,
),
),
),
AnimatedTextExample(
label: 'Typer',
color: Colors.lightGreen[800],
child: SizedBox(
width: 250.0,
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 30.0,
fontFamily: 'Bobbers',
),
child: AnimatedTextKit(
animatedTexts: [
TyperAnimatedText('It is not enough to do your best,'),
TyperAnimatedText('you must know what to do,'),
TyperAnimatedText('and then do your best'),
TyperAnimatedText('- W.Edwards Deming'),
],
onTap: onTap,
),
),
),
),
AnimatedTextExample(
label: 'Typewriter',
color: Colors.teal[700],
child: SizedBox(
width: 250.0,
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 30.0,
fontFamily: 'Agne',
),
child: AnimatedTextKit(
animatedTexts: [
TypewriterAnimatedText('Discipline is the best tool'),
TypewriterAnimatedText('Design first, then code', cursor: '|'),
TypewriterAnimatedText('Do not patch bugs out, rewrite them',
cursor: '<|>'),
TypewriterAnimatedText('Do not test bugs out, design them out',
cursor: '💡'),
],
onTap: onTap,
),
),
),
),
AnimatedTextExample(
label: 'Scale',
color: Colors.blue[700],
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 70.0,
fontFamily: 'Canterbury',
),
child: AnimatedTextKit(
animatedTexts: [
ScaleAnimatedText('Think'),
ScaleAnimatedText('Build'),
ScaleAnimatedText('Ship'),
],
onTap: onTap,
),
),
),
AnimatedTextExample(
label: 'Colorize',
color: Colors.blueGrey[50],
child: AnimatedTextKit(
animatedTexts: [
ColorizeAnimatedText(
'Larry Page',
textStyle: _colorizeTextStyle,
colors: _colorizeColors,
),
ColorizeAnimatedText(
'Bill Gates',
textStyle: _colorizeTextStyle,
colors: _colorizeColors,
),
ColorizeAnimatedText(
'Steve Jobs',
textStyle: _colorizeTextStyle,
colors: _colorizeColors,
),
],
onTap: onTap,
),
),
AnimatedTextExample(
label: 'TextLiquidFill',
color: Colors.white,
child: TextLiquidFill(
text: 'LIQUIDY',
waveColor: Colors.blueAccent,
boxBackgroundColor: Colors.redAccent,
textStyle: const TextStyle(
fontSize: 70,
fontWeight: FontWeight.bold,
),
boxHeight: 300,
),
),
AnimatedTextExample(
label: 'Wavy Text',
color: Colors.purple,
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 20.0,
),
child: AnimatedTextKit(
animatedTexts: [
WavyAnimatedText(
'Hello World',
textStyle: const TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold,
),
),
WavyAnimatedText('Look at the waves'),
WavyAnimatedText('They look so Amazing'),
],
onTap: onTap,
),
),
),
AnimatedTextExample(
label: 'Flicker',
color: Colors.pink[300],
child: DefaultTextStyle(
style: const TextStyle(
fontSize: 35,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 7.0,
color: Colors.white,
offset: Offset(0, 0),
),
],
),
child: AnimatedTextKit(
repeatForever: true,
animatedTexts: [
FlickerAnimatedText('Flicker Frenzy'),
FlickerAnimatedText('Night Vibes On'),
FlickerAnimatedText("C'est La Vie !"),
],
onTap: onTap,
),
),
),
AnimatedTextExample(
label: 'Combination',
color: Colors.pink,
child: AnimatedTextKit(
onTap: onTap,
animatedTexts: [
WavyAnimatedText(
'On Your Marks',
textStyle: const TextStyle(
fontSize: 24.0,
),
),
FadeAnimatedText(
'Get Set',
textStyle: const TextStyle(
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
ScaleAnimatedText(
'Ready',
textStyle: const TextStyle(
fontSize: 48.0,
fontWeight: FontWeight.bold,
),
),
RotateAnimatedText(
'Go!',
textStyle: const TextStyle(
fontSize: 64.0,
),
rotateOut: false,
duration: const Duration(milliseconds: 400),
)
],
),
),
];
Step 4: Run
Then run the project.
Reference
Below are the reference links
Number | Link |
---|---|
1. | Download Example |
2. | Read more |
3. | Follow package author |