statemachine 1.6.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

State Machine #

Pub Package Build Status Coverage Status GitHub Issues GitHub Forks GitHub Stars GitHub License

A simple, yet powerful state machine framework for Dart supporting Flutter and web apps.

This library is open source, stable and well tested. Development happens on GitHub. Feel free to report issues or create a pull-request there. General questions are best asked on StackOverflow.

The package is hosted on dart packages. Up-to-date class documentation is created with every release.

Tutorial #

Installation #

Follow the installation instructions on dart packages.

Import the package into your Dart code using:

import 'package:statemachine/statemachine.dart';

Creating a machine #

To create a new state machine instantiate Machine:

var machine = Machine();

Defining states #

To create states call Machine.newState and store them in variables. The first state created it the start state of the machine. Optionally you can provide a name as argument to ease debugging.

var startState = machine.newState();
var activeState = machine.newState('active');

It is possible to explicitly create start and stop states of the machine using Machine.newStartState and Machine.newStopState.

Callbacks on states #

States support callbacks whenever a state is entered or left.

activeState.onEntry(() => print('activated'));
activeState.onExit(() => print('deactivate'));

Starting and stopping a machine #

To start a state machine and set its state to its starting state call Machine.start:


Similarly you can stop a machine by calling Machine.stop.

Transitioning between states #

There are various ways in which your machine can switch states.

Manually triggered transition #

From anywhere within your code you can enter a specific state by calling State.enter.


Event triggered transition #

You can define transitions between states that are triggered by events of any kind using State.onStream. The example below registers for click events when the inactive state is entered. In case of a click event the callback is executed and the state machine transitions into the active state:

startState.onStream(element.onClick, (value) => activeState.enter());

Future completion transition #

Also, transitions can be triggered by the completion of a future using State.onFuture. Since futures cannot be suspended or cancelled, the future continues to run even if the owning state is deactivated. Should the state be activated and the future value is already present, then the value is immediately supplied into the callback. Further activations have no effect.

startState.onFuture(computation, (value) => activeState.enter());

Time based transition #

Also, you can automatically trigger callbacks after a timeout using State.onTimeout. The following snippet calls the callback 1 second after the active state is entered and falls back to the inactive state:

activeState.onTimeout(Duration({seconds: 1}), () => startState.enter());

Callbacks often contain code to check for additional constraints and update other objects or UI element before entering a different state. See the tooltip example directory for a more complete illustration of the functionality provided by this library.

Nested machines #

Machines can be nested. Simply add another machine that gets started when the state is entered, and stopped when the state is left.


Misc #

Resources #

License #

The MIT License, see LICENSE.

Changelog #

1.6.0 #

  • Dart 2.3 compatibility and requirement.

1.5.0 #

  • Dart 2.2 compatibility and requirement.

1.4.0 #

  • Drop Dart 1.0 compatibility.

1.3.0 #

  • Reformat all code.
  • Reorganize into micro libraries.

1.2.3 #

  • Replace deprecated async code.

1.2.2 #

  • Fix CSS problem in example.

1.2.1 #

  • Update documentation.

1.2.0 #

  • Fix linter warnings.
  • Update documentation.
  • Address missing coverage.

1.1.1 #

  • Fix a broken test.

1.1.0 #

  • Update to use Travis.

1.0.6 #

  • Support for nested machines.
  • Support for enter/exist state events.

1.0.3 #

  • Improve test coverage.

1.0.0 #

  • Initial version.


State Machine Examples #

This package contains examples to illustrate the use of StateMachine. A tutorial and full documentation is contained in the package description and API documentation.

Custom Tooltip #

To run the tooltip example execute the following commands from the command line and navigate to http://localhost:8080/tooltip/example.html:

pub global activate webdev
webdev serve

Traffic Lights #

To run the traffic lights example execute dart example/traffic/traffic.dart from the command line.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  statemachine: ^1.6.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:statemachine/statemachine.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 25, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
Dev dependencies
build_runner ^1.4.0
build_web_compilers ^2.0.0
test ^1.6.0