Skip to content

Instantly share code, notes, and snippets.

@divyanshub024
Last active November 18, 2020 01:35
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save divyanshub024/62a136be30c44fb95db692117a0f6c6a to your computer and use it in GitHub Desktop.
Save divyanshub024/62a136be30c44fb95db692117a0f6c6a to your computer and use it in GitHub Desktop.
Flutter Circular Wave Canvas Animation
import 'dart:math' as math;
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Wave Animation',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CircleWaveRoute(),
);
}
}
class CircleWaveRoute extends StatefulWidget {
@override
_CircleWaveRouteState createState() => _CircleWaveRouteState();
}
class _CircleWaveRouteState extends State<CircleWaveRoute>
with SingleTickerProviderStateMixin {
double waveRadius = 0.0;
double waveGap = 10.0;
Animation<double> _animation;
AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: Duration(milliseconds: 1500), vsync: this);
controller.forward();
controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.reset();
} else if (status == AnimationStatus.dismissed) {
controller.forward();
}
});
}
@override
Widget build(BuildContext context) {
_animation = Tween(begin: 0.0, end: waveGap).animate(controller)
..addListener(() {
setState(() {
waveRadius = _animation.value;
});
});
return Scaffold(
backgroundColor: Colors.white,
body: CustomPaint(
size: Size(double.infinity, double.infinity),
painter: CircleWavePainter(waveRadius),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
class CircleWavePainter extends CustomPainter {
final double waveRadius;
var wavePaint;
CircleWavePainter(this.waveRadius) {
wavePaint = Paint()
..color = Colors.black
..style = PaintingStyle.stroke
..strokeWidth = 2.0
..isAntiAlias = true;
}
@override
void paint(Canvas canvas, Size size) {
double centerX = size.width / 2.0;
double centerY = size.height / 2.0;
double maxRadius = hypot(centerX, centerY);
var currentRadius = waveRadius;
while (currentRadius < maxRadius) {
canvas.drawCircle(Offset(centerX, centerY), currentRadius, wavePaint);
currentRadius += 10.0;
}
}
@override
bool shouldRepaint(CircleWavePainter oldDelegate) {
return oldDelegate.waveRadius != waveRadius;
}
double hypot(double x, double y) {
return math.sqrt(x * x + y * y);
}
}
@divyanshub024
Copy link
Author

wave_anim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment