Created
December 25, 2020 12:34
-
-
Save imaNNeo/1d96ef275e2aa786b7a49466a5870550 to your computer and use it in GitHub Desktop.
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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'dart:math' as math; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter4Fun.com', | |
theme: ThemeData( | |
primarySwatch: Colors.yellow, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({Key key}) : super(key: key); | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
double degree = 0.0; | |
Timer timer; | |
@override | |
void initState() { | |
super.initState(); | |
timer = new Timer.periodic( | |
Duration(milliseconds: 16), | |
(t) { | |
setState(() { | |
degree += 1; | |
}); | |
}, | |
); | |
} | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Color(0xFF191636), | |
body: Center( | |
child: CustomPaint( | |
painter: _OrbitPainter(degree), | |
size: Size(300, 100), | |
), | |
), | |
); | |
} | |
@override | |
void dispose() { | |
timer.cancel(); | |
super.dispose(); | |
} | |
} | |
class _OrbitPainter extends CustomPainter { | |
final double electronDegree; | |
_OrbitPainter(this.electronDegree); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final center = Offset(size.width / 2, size.height / 2); | |
final rect = Rect.fromCenter(center: center, width: size.width, height: size.height); | |
canvas.drawOval( | |
rect, | |
Paint() | |
..color = Colors.white | |
..style = PaintingStyle.stroke | |
..strokeWidth = 1, | |
); | |
final degree = degreesToRads(electronDegree); | |
final electronPos = center + | |
Offset( | |
math.cos(degree) * (size.width / 2), | |
math.sin(degree) * (size.height / 2), | |
); | |
canvas.drawCircle(electronPos, 8, Paint()..color = Colors.yellowAccent); | |
} | |
@override | |
bool shouldRepaint(covariant CustomPainter oldDelegate) => true; | |
} | |
num degreesToRads(num deg) { | |
return (deg * math.pi) / 180.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment