Skip to content

Instantly share code, notes, and snippets.

@kumar-aakash86
Created February 20, 2024 10:32
Show Gist options
  • Save kumar-aakash86/ea524278b513216d260bbb393591e2d3 to your computer and use it in GitHub Desktop.
Save kumar-aakash86/ea524278b513216d260bbb393591e2d3 to your computer and use it in GitHub Desktop.
Animated canvas arrows - flutter
import 'package:flutter/material.dart';
// Animated canvas arrows
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
AnimatedArrows(),
Text('Hello, World!'),
]),
),
),
);
}
}
class AnimatedArrows extends StatefulWidget {
@override
State<AnimatedArrows> createState() => _AnimatedArrowsState();
}
class _AnimatedArrowsState extends State<AnimatedArrows>
with TickerProviderStateMixin {
late Animation<double> animation;
late AnimationController controller;
Tween<double> _rotationTween = Tween(begin: 0, end: 2);
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
duration: Duration(milliseconds: 1000),
);
animation = _rotationTween.animate(controller)
..addListener(() {
setState(() {});
})
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.repeat();
} else if (status == AnimationStatus.dismissed) {
controller.forward();
}
});
controller.forward();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return CustomPaint(
size: Size(40, 40),
painter: ArrowPainter(value: (controller.value * 100).toInt()),
);
}
}
class ArrowPainter extends CustomPainter {
final int value;
ArrowPainter({required this.value});
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.grey[300]!
..style = PaintingStyle.stroke
..strokeWidth = 3;
var halfWidth = size.width/2;
var halfHeight = size.height/2;
var path = Path();
path.moveTo(halfWidth - 5, halfHeight-12);
path.lineTo(halfWidth, halfHeight-7);
path.lineTo(halfWidth + 5, halfHeight - 12);
canvas.drawPath(path,paint..color = (value< 33 ? Colors.green : Colors.green.shade100));
path = Path();
path.moveTo(halfWidth - 7, halfHeight-7);
path.lineTo(halfWidth, halfHeight);
path.lineTo(halfWidth + 7, halfHeight - 7);
canvas.drawPath(path,paint..color = (value> 33 && value<66 ? Colors.green : Colors.green.shade100));
path = Path();
path.moveTo(halfWidth - 9, halfHeight - 2);
path.lineTo(halfWidth, halfHeight+7);
path.lineTo(halfWidth + 9, halfHeight - 2 );
canvas.drawPath(path,paint..color = (value> 66 ? Colors.green : Colors.green.shade100));
}
@override
bool shouldRepaint(ArrowPainter oldDelegate) => oldDelegate.value != value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment