Flutter Animate Color
Flutter Animate Color
In this tutorial, we will learn how to animate color of a widget, i.e., transitioning from a starting color to ending
color.
You can animate color of a widget using ColorTween.
In the following example, we shall animate color of a button, when it is pressed.
[Link]
import 'package:flutter/[Link]';
import 'package:flutter/[Link]';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
Animation<Color> animation;
AnimationController controller;
@override
void initState() {
[Link]();
controller =
AnimationController(duration: const Duration(seconds: 1), vsync: this);
animation =
ColorTween(begin: [Link], end: [Link]).animate(controller)
..addListener(() {
setState(() {
// The state that has changed here is the animation object’s value.
});
});
}
void animateColor() {
[Link]();
}
@override
Widget build(BuildContext context) {
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Center(child: Text('Flutter - [Link]')),
),
body: ListView(children: <Widget>[
Container(
margin: [Link](10),
padding: [Link](20),
height: 400,
child: RaisedButton(
onPressed: () => {animateColor()},
color: [Link],
child: Text(''),
))
]),
));
}
@override
void dispose() {
[Link]();
[Link]();
}
}
When you run this application, and click on the big square button which is colored indigo, animation of color
starts and changes the color of button to limegreen.
Now let us modify the application, such that the color toggles when button is pressed.
We shall use [Link]() function to reverse the animation.
[Link]
import 'package:flutter/[Link]';
import 'package:flutter/[Link]';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
Animation<Color> animation;
AnimationController controller;
@override
void initState() {
[Link]();
controller =
AnimationController(duration: const Duration(seconds: 1), vsync: this);
animation =
ColorTween(begin: [Link], end: [Link]).animate(controller)
..addListener(() {
setState(() {
// The state that has changed here is the animation object’s value.
});
});
}
bool buttonToggle = true;
void animateColor() {
if (buttonToggle) {
[Link]();
} else {
[Link]();
}
buttonToggle = !buttonToggle;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Center(child: Text('Flutter - [Link]')),
),
body: ListView(children: <Widget>[
Container(
margin: [Link](10),
padding: [Link](20),
height: 400,
child: RaisedButton(
onPressed: () => {animateColor()},
color: [Link],
child: Text(''),
))
]),
));
}
@override
void dispose() {
[Link]();
[Link]();
}
}
When you run this application and click on the colored button, color animation toggles using [Link]()
and [Link]().
Conclusion
In this Flutter Tutorial, we learned how to animate Color property using Animation and AnimationController
classes.
Flutter Tutorial
✦ Flutter Tutorial
✦ Flutter - Install on Linux - Ubuntu
✦ Flutter - Basic Application Example
Flutter Widgets
✦ Flutter Text
✦ Flutter TextField
✦ Flutter FlatButton
✦ Flutter RaisedButton
✦ Flutter SnackBar
✦ Flutter Switch
✦ Flutter ToggleButtons
✦ Flutter Table
✦ Flutter DataTable
✦ Flutter Tooltip
✦ Flutter TabBar & TabBarView
Flutter Animation
✦ Flutter Animation Basic Example
➩ Flutter Animate Color
Flutter Packages
✦ Flutter sqflite - SQLite Tutorial
Flutter Examples
Flutter Examples
✦ Flutter Login Screen Sample