Created with <3 with dartpad.dev.
Created
September 26, 2023 15:28
-
-
Save stephanedeluca/32e89ff2a187290742feb06106a49411 to your computer and use it in GitHub Desktop.
hollow-spray-0532
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
import 'package:flutter/material.dart'; | |
const 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 StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Text( | |
'Hello, World!', | |
style: Theme.of(context).textTheme.headlineMedium, | |
); | |
} | |
} | |
class FanWidget extends StatefulWidget { | |
/// Fan color | |
final Color color; | |
/// Background color | |
final Color? backgroundColor; | |
/// Size (Diameter) | |
final double size; | |
/// Rotation period | |
final Duration period; | |
/// Whether to animate the fan | |
final bool powerOn; | |
const FanWidget({ | |
super.key, | |
required this.color, | |
this.backgroundColor, | |
this.size = 54, | |
this.period = const Duration(seconds: 1), | |
required this.powerOn, | |
}); | |
@override | |
State<FanWidget> createState() => _FanWidgetState(); | |
} | |
class _FanWidgetState extends State<FanWidget> | |
with SingleTickerProviderStateMixin { | |
double speed = 0; | |
static const m = 1; //kg | |
double _lastTime = DateTime.now().millisecond.toDouble(); | |
double f = 0; | |
double s = 0; | |
double p = 0; | |
late final ctrl = AnimationController.unbounded(vsync: this); | |
static const _period = 0.150; | |
late final timer = | |
Timer.periodic(Duration(milliseconds: 150), (t) => physics(t)); | |
void physics(Timer timer) { | |
// DT in seconds | |
final dt = _period; //max(t - _lastTime, 1) * 1e-3; | |
double motor = 0; | |
if (widget.powerOn) { | |
final motorPower = 1.0; | |
//period.inMilliseconds * 1e-3; | |
motor = 1 * motorPower; | |
} else { | |
// break | |
f *= 0.5; | |
} | |
final airResistance = 0; | |
//-s * dt; | |
f += motor + airResistance; | |
final a = f / m; | |
s += .5 * a * dt; | |
p += s * dt; | |
ctrl.value = p / 10; | |
// if (s <= 0) | |
// powerOn = false; | |
// else | |
// period = Duration(milliseconds: (1000 / p).round()); | |
debugPrint( | |
"physics: dt:${dt.toStringAsFixed(1)} |f: ${f.toStringAsFixed(1)} | a:${a.toStringAsFixed(1)}|s: ${s.toStringAsFixed(1)}|p: ${p.toStringAsFixed(1)}"); //|period:${period.inMilliseconds}|powerOn: $powerOn"); | |
} | |
@override | |
void initState() { | |
_lastTime = DateTime.now().millisecond.toDouble(); | |
// ctrl.value = 200; | |
// ctrl.animateWith( | |
// FrictionSimulation( | |
// 0.55, // <- the bigger this value, the less friction is applied | |
// ctrl.value, | |
// 100 /*period.inMilliseconds */ // <- Velocity of inertia | |
// ), | |
// ); | |
//f = m * a; | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
timer.cancel(); | |
ctrl.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
//final t = DateTime.now().millisecond.toDouble(); | |
final color = widget.color; | |
final size = widget.size; | |
var powerOn = widget.powerOn; | |
var period = widget.period; | |
final fan =Container(width:30, height:30), color: Colors.red); | |
return//period.inMilliseconds > 1 | |
powerOn | |
? RotatingWidget(period: period, child: fan) | |
//Transform.rotate(angle: ctrl.value, child: fan) | |
: fan, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment