This example creates a blue square that fades in and out continuously using AnimationController.
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: Scaffold(
body: Center(
child: SimpleAnimation(),
),
),
);
}
}
class SimpleAnimation extends StatefulWidget {
const SimpleAnimation({super.key});
@override
State<SimpleAnimation> createState() => _SimpleAnimationState();
}
class _SimpleAnimationState extends State<SimpleAnimation> with SingleTickerProviderStateMixin {
late final AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 2),
)..repeat(reverse: true);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Opacity(
opacity: _controller.value,
child: Container(
width: 200,
height: 200,
color: Colors.blue,
),
);
},
);
}
}