Skip to content

Instantly share code, notes, and snippets.

@imaNNeo
Last active December 25, 2020 12:34
Show Gist options
  • Save imaNNeo/614dbea2d7a17e2e046a003d58609e3f to your computer and use it in GitHub Desktop.
Save imaNNeo/614dbea2d7a17e2e046a003d58609e3f to your computer and use it in GitHub Desktop.
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;
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF191636),
body: Center(
child: Column(
children: [
Expanded(child: Container()),
CustomPaint(
painter: _OrbitPainter(degree),
size: Size(300, 100),
),
Expanded(child: Container()),
Text('${degree.toInt().toString()}°', style: TextStyle(color: Colors.white),),
Slider(
value: degree,
min: 0.0,
max: 360.0,
onChanged: (newValue) {
setState(() {
degree = newValue;
});
},
)
],
),
),
);
}
}
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