Created
January 22, 2020 13:37
-
-
Save akshatapp/afe4b27688c1b2fa38b426ed0d25d669 to your computer and use it in GitHub Desktop.
Flutter Paint Line Rotate 360 Demo
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
// Code Licensed under the Apache License, Version 2.0 - visit : https://github.com/akshatapp/flutter-gist/blob/master/LICENSE | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Paint Line Rotate 360 Demo', | |
debugShowCheckedModeBanner: false, | |
// showPerformanceOverlay: true, | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> | |
with SingleTickerProviderStateMixin { | |
AnimationController _animationController; | |
@override | |
void initState() { | |
super.initState(); | |
_animationController = | |
AnimationController(duration: Duration(seconds: 15), vsync: this) | |
..repeat(); | |
} | |
@override | |
void dispose() { | |
_animationController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final lineLength = getLineLength(context); | |
return Scaffold( | |
backgroundColor: Colors.white, | |
body: Center( | |
child: RotationTransition( | |
turns: _animationController, | |
child: CustomPaint( | |
painter: MyPaint(lineLength), | |
), | |
), | |
), | |
); | |
} | |
} | |
class MyPaint extends CustomPainter { | |
final length; | |
MyPaint(this.length); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final center = (Offset.zero & size).center; | |
final position = center + Offset(0.0, -1.0) * length; | |
final linePaint = Paint() | |
..color = Colors.red | |
..strokeWidth = 4.0 | |
..strokeCap = StrokeCap.round; | |
canvas.drawLine(center, position, linePaint); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) { | |
return false; | |
} | |
} | |
double getLineLength(context) { | |
final shortSide = MediaQuery.of(context).size.shortestSide; | |
return (shortSide * 0.25); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment