https://github.com/kerberjg/dart_coroutines
Unity-style Coroutines for Flutter/Dart
https://github.com/kerberjg/dart_coroutines
async coroutines gamedev resumable widgets
Last synced: 8 months ago
JSON representation
Unity-style Coroutines for Flutter/Dart
- Host: GitHub
- URL: https://github.com/kerberjg/dart_coroutines
- Owner: kerberjg
- License: mpl-2.0
- Created: 2025-09-13T22:19:57.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-10-05T21:48:22.000Z (8 months ago)
- Last Synced: 2025-10-05T23:33:37.781Z (8 months ago)
- Topics: async, coroutines, gamedev, resumable, widgets
- Language: Dart
- Homepage: https://pub.dev/packages/coroutines
- Size: 21.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# coroutines
Unity-style Coroutines for Flutter/Dart, implementing resumable functions.
# Features
- Lightweight and easy to use
- Supports both synchronous and asynchronous coroutines
- Can be easily integrated into existing Flutter/Dart applications
# Installation
Add the following dependency to your `pubspec.yaml` file:
```yaml
dependencies:
coroutines: ^0.1.0
```
# Examples
## With widgets
In this example, a StatefulWidget uses the CoroutineExecutor mixin to run a coroutine that updates every time
a button is pressed.
```dart
import 'package:flutter/material.dart';
import 'package:coroutines/coroutines.dart';
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State with CoroutineExecutor {
int _counter = 0;
void _incrementCounter() {
runCoroutine(_myCoroutine);
}
CoroutineValue _myCoroutine() sync* {
while (true) {
yield _counter++;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Coroutine Example'),
),
body: Center(
child: Text('Counter: $_counter'),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
child: Icon(Icons.add),
),
);
}
}
```
## Game objects
```dart
class JumpingCube extends GameObject with CoroutineExecutor {
double jumpSpeed = 10;
double fallSpeed = -5
bool isOnGround() {
// Check if the cube is on the ground
}
/// Started when a jump button is pressed, iterates every frame to continue the jump animation
CoroutineValue _jumpCoroutine() sync* {
double verticalSpeed = jumpSpeed;
while(!isOnGround()) {
verticalSpeed -= 0.25; // apply gravity
verticalSpeed = verticalSpeed.clamp(fallSpeed, double.infinity); // clamp to terminal velocity
position.y += verticalSpeed; // move the cube
yield true;
}
return false; // end of jump
}
@override
void onKeyPressed(KeyEvent event) {
// "An A-press is an A-press, you can't say it's only a half" - well, TJ "Henry" Yoshi...
if(event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.keyA) {
addCoroutine(_jumpCoroutine);
}
}
@override
void update(double deltaTime) {
runAllCoroutines();
}
}