Last active
April 1, 2020 21:40
-
-
Save MarkTechson/afb0e5725d2217e75f853a40cf0da43c to your computer and use it in GitHub Desktop.
Loading Animation with a Custom Painter
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'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark() | |
.copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatefulWidget { | |
@override | |
_MyWidgetState createState() => _MyWidgetState(); | |
} | |
class _MyWidgetState extends State<MyWidget> | |
with SingleTickerProviderStateMixin { | |
AnimationController animation; | |
double turn = 0; | |
@override | |
void initState() { | |
super.initState(); | |
// create the animation controller | |
animation = AnimationController( | |
vsync: this, | |
duration: Duration(seconds: 20), | |
) | |
..addListener(() { | |
setState(() => turn = animation.value); | |
})..forward(); | |
} | |
@override | |
void dispose() { | |
animation.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return CustomPaint( | |
size: Size(200, 200), | |
painter: ArcPainter(angle: turn), | |
); | |
} | |
} | |
class ArcPainter extends CustomPainter { | |
final double angle; | |
ArcPainter({this.angle}); | |
@override | |
bool shouldRepaint(ArcPainter _) { | |
return angle != _.angle; | |
} | |
@override | |
void paint(Canvas canvas, Size size) { | |
var p = Paint() | |
..color = Colors.blueGrey | |
..strokeWidth = 10.0 | |
..style = PaintingStyle.stroke; | |
var rect = Rect.fromLTWH(0.0, 0.0, size.width / 2, size.height / 2,); | |
canvas.drawArc( | |
rect, | |
2 * pi, | |
angle * (2 * pi), | |
false, | |
p); | |
} | |
degToRad(double degrees) => degrees * pi / 180.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment