Created
June 3, 2019 11:00
-
-
Save Abdullamhd/95daed1dd360d1b1b0678605ff6c42b2 to your computer and use it in GitHub Desktop.
circle resizing animation in flutter
This file contains hidden or 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 'package:flutter/material.dart'; | |
import 'dart:math' as Math; | |
class AnimationTest extends StatefulWidget { | |
@override | |
_AnimationTestState createState() => _AnimationTestState(); | |
} | |
class _AnimationTestState extends State<AnimationTest> | |
with SingleTickerProviderStateMixin { | |
AnimationController _controller; | |
Animation animation; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
vsync: this, duration: Duration(seconds: 10)); | |
_controller.addListener(() { | |
}); | |
animation = Tween( | |
begin: 0.0, | |
end: Math.pi * 20 , | |
).animate(_controller) | |
..addListener(() { | |
setState(() {}); | |
}); | |
_controller.addStatusListener((state){ | |
if(state == AnimationStatus.completed){ | |
_controller.forward(); | |
} | |
}); | |
_controller.forward(); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: CustomPaint( | |
painter: TestPainter( | |
pi: animation.value | |
), | |
child: Container( | |
height: double.infinity, | |
), | |
)); | |
} | |
} | |
class TestPainter extends CustomPainter { | |
final double pi ; | |
TestPainter({this.pi}); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final height = size.height; | |
final width = size.width; | |
// canvas.translate(0, height); | |
// canvas.scale(1, -1); | |
Paint paint = Paint() | |
..style = PaintingStyle.fill | |
..color = Colors.green | |
..strokeWidth = 10; | |
var centerY = height * .5 , | |
centerX = width * .5 , | |
offset = 50 , | |
speed = height * .001 , | |
baseRadius = 50 + Math.sin(pi) * offset; | |
canvas.drawCircle(Offset(centerX, centerY ), baseRadius, paint); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) => this != oldDelegate; | |
void render({double x, double y, Canvas canvas , Paint paint }) { | |
canvas.drawRect(Rect.fromLTWH(x, y, .5, .5), paint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment