Flutter Heyteacher Timer Workflow
This package provides functionalities to manage timer-based workflows for the Heyteacher project.
Features
This package exports the following modules via timer_workflow.dart:
- Workflow Management:
TimerWorkflow: The core class for managing the lifecycle of a timer workflow.WorkflowStatus: Defines the current state of the workflow.
- Tasks:
TimerTask: Represents a specific task definition within the workflow.RunningTask: Represents an active instance of a task.
- Localization:
FlutterHeyteacherTimerWorkflowLocalizationsprovides localized strings for workflow-related UI. - Error Handling:
WorkflowTaskAlreadyInitializedWorkflowTaskNotInitialized
The action available on a workflow are:
start(): start the workflowstop(): stop the workflowpause(): pause the workflowrestart(): restart the workflownext(): go to the next task
The stream of the workflow is a Stream<RunningTask<T>> is emitting every second with running task data:
workflowName: the name of the workflowstatus: the current status of the workflowcurrent: the current tasknext: the next taskchanged: if the task has just changedtaskElapsedTime: the elapsed time of the current tasktotalElapsedTime: the total elapsed time of completed tasks
Usage
Define your workflow by extending TimerWorkflow<T>:
class MyWorkflow extends TimerWorkflow<MyTask> {
@override
String get name => 'My Workflow';
@override
void initializeTasks() => tasks.addAll([
MyTask(
name: 'Task 1',
description: 'First Task',
duration: const Duration(seconds: 10),
),
MyTask(
name: 'Task 2',
description: 'Second Task',
duration: const Duration(seconds: 20),
),
MyTask(
name: 'Task 3',
description: 'Third Task',
duration: const Duration(seconds: 15),
),
]);
}
Define your task by extending TimerTask:
class MyTask extends TimerTask {
const MyTask({
required super.name,
required super.description,
required super.duration,
});
}
Start the workflow:
_workflow.start();
Listen to the workflow changes:
_workflow.stream.listen((runningTask) {
if (runningTask.changed) {
print('Task ${runningTask.current?.name} completed');
if (runningTask.next != null) {
print('Task ${runningTask.next?.name} started');
} else {
print('All done');
}
}
});
A complete app example can be found in example