الحركات المتقدمة — Flutter المتقدم
الحركات المتقدمة في Flutter
1. AnimationController
class FadeWidget extends StatefulWidget {
@override
_FadeWidgetState createState() => _FadeWidgetState();
}
class _FadeWidgetState extends State<FadeWidget>
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: 1).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
),
);
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _animation,
child: Text('مرحباً!'),
);
}
}
2. حركات متعددة
class MultiAnimationWidget extends StatefulWidget {
@override
_MultiAnimationWidgetState createState() => _MultiAnimationWidgetState();
}
class _MultiAnimationWidgetState extends State<MultiAnimationWidget>
with TickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _scaleAnimation;
late Animation<double> _rotationAnimation;
late Animation<Offset> _slideAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(milliseconds: 1500),
vsync: this,
);
_scaleAnimation = Tween<double>(begin: 0.5, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(0.0, 0.5, curve: Curves.easeOut),
),
);
_rotationAnimation = Tween<double>(begin: 0, end: 2 * 3.14159).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(0.3, 0.8, curve: Curves.easeInOut),
),
);
_slideAnimation = Tween<Offset>(
begin: Offset(0, 1),
end: Offset.zero,
).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(0.5, 1.0, curve: Curves.elasticOut),
),
);
_controller.forward();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return SlideTransition(
position: _slideAnimation,
child: Transform.rotate(
angle: _rotationAnimation.value,
child: Transform.scale(
scale: _scaleAnimation.value,
child: child,
),
),
);
},
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
);
}
}
3. Hero Animation
// الشاشة الأولى
GestureDetector(
onTap: () => Navigator.push(
context,
MaterialPageRoute(builder: (_) => DetailScreen(imageUrl: url)),
),
child: Hero(
tag: 'image-$url',
child: Image.network(url, width: 100, height: 100),
),
)
// الشاشة الثانية
class DetailScreen extends StatelessWidget {
final String imageUrl;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Hero(
tag: 'image-$imageUrl',
child: Image.network(imageUrl),
),
),
);
}
}
4. AnimatedContainer
class AnimatedBox extends StatefulWidget {
@override
_AnimatedBoxState createState() => _AnimatedBoxState();
}
class _AnimatedBoxState extends State<AnimatedBox> {
bool _expanded = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => setState(() => _expanded = !_expanded),
child: AnimatedContainer(
duration: Duration(milliseconds: 500),
curve: Curves.easeInOut,
width: _expanded ? 200 : 100,
height: _expanded ? 200 : 100,
decoration: BoxDecoration(
color: _expanded ? Colors.blue : Colors.red,
borderRadius: BorderRadius.circular(_expanded ? 20 : 100),
),
),
);
}
}
5. Lottie Animations
dependencies:
lottie: ^2.6.0
import 'package:lottie/lottie.dart';
// من ملف محلي
Lottie.asset('assets/animations/loading.json')
// من الإنترنت
Lottie.network('https://assets.lottiefiles.com/animation.json')
// مع تحكم
class LottieExample extends StatefulWidget {
@override
_LottieExampleState createState() => _LottieExampleState();
}
class _LottieExampleState extends State<LottieExample>
with TickerProviderStateMixin {
late AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this);
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
if (_controller.isAnimating) {
_controller.stop();
} else {
_controller.forward(from: 0);
}
},
child: Lottie.asset(
'assets/animations/heart.json',
controller: _controller,
onLoaded: (composition) {
_controller.duration = composition.duration;
},
),
);
}
}
اختبر فهمك