Skip to content

Instantly share code, notes, and snippets.

@ashutoshsr7
Created May 30, 2024 10:43
Show Gist options
  • Select an option

  • Save ashutoshsr7/72b1a043756de652add8212bceca0718 to your computer and use it in GitHub Desktop.

Select an option

Save ashutoshsr7/72b1a043756de652add8212bceca0718 to your computer and use it in GitHub Desktop.
Alien UFO - Custom Animations
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: MyHomePage(),
));
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
final Image starsBackground = Image.network(
'https://images.unsplash.com/photo-1531742896094-1424584e750b?q=80&w=1888&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
fit: BoxFit.cover,
);
final Image ufo = Image.network(
'https://img.icons8.com/?size=100&id=YxY4BUR02xKl&format=png&color=000000',
width: 100,
height: 100,
);
final Image cow = Image.network(
'https://img.icons8.com/?size=100&id=9IHYMlaJkuiP&format=png&color=000000',
width: 80,
height: 80,
);
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 5),
vsync: this,
)..repeat(reverse: true);
_animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
Positioned(
top: 0,
left: 0,
right: 0,
bottom: 0,
child: starsBackground,
),
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Positioned(
top: (_animation.value * 200) - 800,
child: ClipPath(
clipper: BeamClipper(_animation.value),
child: Container(
height: 2000,
width: 900,
decoration: BoxDecoration(
gradient: RadialGradient(
radius: 2.0,
colors: [
Colors.yellow.withOpacity(1.0 - _animation.value),
Colors.transparent,
],
),
),
),
),
);
},
),
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Positioned(
top: 100 + (_animation.value * 200),
child: ufo,
);
},
),
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Positioned(
bottom: 30 + (_animation.value * 200),
child: Opacity(
opacity: 1.0 - _animation.value,
child: cow,
),
);
},
),
],
),
);
}
}
class BeamClipper extends CustomClipper<Path> {
final double animationValue;
BeamClipper(this.animationValue);
@override
Path getClip(Size size) {
return Path()
..moveTo(size.width / 2, size.height / 2)
..lineTo(size.width / 2 + (animationValue * size.width / 2), size.height)
..lineTo(size.width / 2 - (animationValue * size.width / 2), size.height)
..close();
}
@override
bool shouldReclip(CustomClipper oldClipper) => true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment