65 phút
Flutter Animations & Advanced UI
Giới thiệu Animations
Tạo trải nghiệm người dùng mượt mà với animations.
Implicit Animations
AnimatedContainer(
duration: Duration(milliseconds: 300),
width: _isExpanded ? 200 : 100,
height: _isExpanded ? 200 : 100,
color: _isExpanded ? Colors.blue : Colors.red,
curve: Curves.easeInOut,
),
AnimatedOpacity(
duration: Duration(milliseconds: 500),
opacity: _isVisible ? 1.0 : 0.0,
child: Text('Fading Text'),
),
Explicit Animations
class RotationAnimation extends StatefulWidget {
@override
_RotationAnimationState createState() => _RotationAnimationState();
}
class _RotationAnimationState extends State<RotationAnimation>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(seconds: 2),
vsync: this,
);
_animation = Tween<double>(
begin: 0,
end: 2 * 3.14159, // 360 degrees in radians
).animate(_controller);
_controller.repeat();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Transform.rotate(
angle: _animation.value,
child: Icon(Icons.refresh, size: 50),
);
},
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
Hero Animations
// First screen
Hero(
tag: 'image-hero',
child: Image.network('https://example.com/image.jpg'),
)
// Second screen
Hero(
tag: 'image-hero',
child: Image.network('https://example.com/image.jpg'),
)
Custom Paint
class CustomCirclePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
canvas.drawCircle(
Offset(size.width / 2, size.height / 2),
size.width / 2,
paint,
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
Page Transitions
Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => NextScreen(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return SlideTransition(
position: Tween<Offset>(
begin: Offset(1.0, 0.0),
end: Offset.zero,
).animate(animation),
child: child,
);
},
),
);