Created
April 11, 2023 00:02
-
-
Save roipeker/6afb50d6e9695cbe30408c56aee90775 to your computer and use it in GitHub Desktop.
Creating Particles with Flutter and GraphX #short
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
/// roipeker 2023. | |
/// | |
/// #short tutorial: https://youtu.be/I8Yo9jq4hXs | |
/// | |
import 'package:flutter/material.dart'; | |
import 'package:graphx/graphx.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
final onCounterChanged = ValueNotifier(0); | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
onCounterChanged.value = _counter; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
], | |
), | |
), | |
floatingActionButton: SceneBuilderWidget( | |
builder: () => SceneController(back: ParticlesScene(onCounterChanged)), | |
child: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
), | |
); | |
} | |
} | |
class ParticlesScene extends GSprite { | |
final ValueNotifier onCounterChanged; | |
ParticlesScene(this.onCounterChanged); | |
@override | |
void dispose() { | |
onCounterChanged.removeListener(_onCounterChanged); | |
super.dispose(); | |
} | |
late GSimpleParticleSystem particles; | |
void _onCounterChanged() { | |
particles.forceBurst(); | |
} | |
@override | |
void addedToStage() { | |
onCounterChanged.addListener(_onCounterChanged); | |
particles = GSimpleParticleSystem(); | |
particles.emit = false; | |
particles.texture = GIcon(Icons.favorite, Colors.white, 32).createImageTextureSync(); | |
addChild(particles); | |
particles.initialColor = Colors.pink.value; | |
particles.endColor = Colors.yellow.value; | |
particles.x = stage!.stageWidth / 2; | |
particles.y = stage!.stageHeight / 2; | |
particles.dispersionAngle = Math.PI; | |
particles.dispersionAngleVariance = Math.PI / 2; | |
particles.initialVelocity = 100; | |
particles.initialVelocityVariance = 100; | |
particles.initialAcceleration = .15; | |
particles.initialAccelerationVariance = .15; | |
particles.emission = 20; | |
particles.endScale = 0.2; | |
particles.endScaleVariance = 0.2; | |
particles.initialAngularVelocity = -.003; | |
particles.initialAngularVelocityVariance = .006; | |
particles.initialAngleVariance = Math.PI / 2; | |
particles.initialScale = .4; | |
particles.initialScaleVariance = .8; | |
particles.endAlpha = 0; | |
particles.endAlphaVariance = .3; | |
particles.blendMode = BlendMode.difference; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment