Last active
November 18, 2020 01:35
-
-
Save divyanshub024/62a136be30c44fb95db692117a0f6c6a to your computer and use it in GitHub Desktop.
Flutter Circular Wave Canvas Animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Author
divyanshub024
commented
May 25, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment