Skip to content

Instantly share code, notes, and snippets.

@MarkTechson
Last active April 1, 2020 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkTechson/afb0e5725d2217e75f853a40cf0da43c to your computer and use it in GitHub Desktop.
Save MarkTechson/afb0e5725d2217e75f853a40cf0da43c to your computer and use it in GitHub Desktop.
Loading Animation with a Custom Painter
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