This app shows a square that smoothly changes color from red to blue and back. It uses a ColorTween with an AnimationController that repeats forever.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: TweenAnimationExample(),
);
}
}
class TweenAnimationExample extends StatefulWidget {
const TweenAnimationExample({super.key});
@override
State<TweenAnimationExample> createState() => _TweenAnimationExampleState();
}
class _TweenAnimationExampleState extends State<TweenAnimationExample> with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<Color?> _colorAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
);
_colorAnimation = ColorTween(begin: Colors.red, end: Colors.blue).animate(_controller);
_controller.repeat(reverse: true);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Tween Animation Example')),
body: Center(
child: AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Container(
width: 100,
height: 100,
color: _colorAnimation.value,
);
},
),
),
);
}
}