Instantly share code, notes, and snippets.

@artrmz artrmz/bubbles.dart
Last active Sep 20, 2018

Embed
What would you like to do?
class Bubbles extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _BubblesState();
}
}
class _BubblesState extends State<Bubbles> with TickerProviderStateMixin {
AnimationController animationController;
final bubbles = <Bubble>[];
@override
void initState() {
super.initState();
List.generate(5, (i) {
bubbles.add(Bubble());
});
animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 1000))
..addListener(() {
for (int i = 0; i < bubbles.length; i++) {
bubbles[i].move();
if (bubbles[i].remainingLife < 0 || bubbles[i].radius < 0) {
bubbles[i] = Bubble();
}
}
})
..repeat();
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animationController,
builder: (context, child) => CustomPaint(
size: Size(64.0, 64.0),
painter: _BubblePainter(bubbles),
),
);
}
}
class _BubblePainter extends CustomPainter {
final List<Bubble> bubbles;
_BubblePainter(this.bubbles);
@override
void paint(Canvas canvas, Size size) {
for (var bubble in bubbles) {
bubble.display(canvas);
}
}
@override
bool shouldRepaint(_BubblePainter oldDelegate) => true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment