Junior
What types of animations are available in Flutter?
sobes.tech AI
Answer from AI
- Tween Animation
- Physics-based Animation
Tween Animation:
Based on interpolating values between the initial and final states. Uses AnimationController, Animation, and Tween.
Physics-based Animation:
Simulates real physical processes such as springs or damping. More pleasant for the user as it feels more realistic. Uses the flutter/physics package.
Examples of widgets for animations:
AnimatedContainerAnimatedOpacityHero(for screen transitions)AnimatedBuilderSlideTransitionScaleTransitionFadeTransition
// Example of Tween animation
import 'package:flutter/material.dart';
class AnimatedContainerExample extends StatefulWidget {
const AnimatedContainerExample({super.key});
@override
State<AnimatedContainerExample> createState() =>
_AnimatedContainerExampleState();
}
class _AnimatedContainerExampleState extends State<AnimatedContainerExample> {
bool _isBig = false;
void _toggleSize() {
setState(() {
_isBig = !_isBig;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('AnimatedContainer Example')),
body: Center(
child: AnimatedContainer(
duration: const Duration(seconds: 1), // Animation duration
curve: Curves.easeInOut, // Animation curve
width: _isBig ? 200.0 : 100.0,
height: _isBig ? 200.0 : 100.0,
color: _isBig ? Colors.blue : Colors.red,
alignment: _isBig ? Alignment.center : Alignment.topLeft,
child: FlutterLogo(size: _isBig ? 100 : 50),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _toggleSize,
tooltip: 'Toggle Size',
child: const Icon(Icons.swap_vert),
),
);
}
}