StateTreeBuilder class
Provides methods to describe a state tree.
States are defined and added to the state tree are described by calling the state, dataState, and finalState methods.
enum Messages { toggle }
var offState = StateKey('off');
var onState = StateKey('on');
StateTreeBuilder switchBuilder() {
// A simple switch with on and off states
var treeBuilder = StateTreeBuilder(initialState: offState)
treeBuilder.state(offState, (b) {
b.onMessageValue(Messages.toggle, (b) => b.goTo(onState));
})
treeBuilder.state(onState, (b) {
b.onMessageValue(Messages.toggle, (b) => b.goTo(offState));
});
return treeBuilder;
}
Once a StateTreeBuilder has been initialized with the desired states, it can be used to create a state machine. The state machine will create and manage its own instance of the state tree defined by the builder.
var stateTreeBuilder = switchBuilder();
var stateMachine = TreeStateMachine(builder);
Note that a single state tree builder instance can be used to create multiple state machine instances.
A textual description of the state tree can be produced by calling format method, passing a StateTreeFormatter (for example a DotFormatter) representing the desired output format.
Constructors
- StateTreeBuilder({required StateKey initialState})
-
Creates a StateTreeBuilder that will build a state tree that starts in the state identified
by
initialState.factory - StateTreeBuilder.withRoot(StateKey rootState, void build(StateBuilder builder), InitialChild initialChild)
-
Creates a StateTreeBuilder with a predefined root state.
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
TreeBuildContext context) → TreeNode -
call(
TreeBuildContext context) → TreeNode -
dataState<
D> (StateKey stateKey, InitialData< D> initialData, void build(DataStateBuilder<D> builder), {StateKey? parent, InitialChild? initialChild, StateDataCodec? codec}) → void -
Adds to the state tree a description of a data state, identified by
stateKeyand carrying a value of typeD. -
finalState(
StateKey stateKey, void build(FinalStateBuilder builder)) → void -
Adds to the state tree a description of a final state, identified by
stateKey. The behavior of the state is configured by thebuildcallback. -
format(
StringSink sink, StateTreeFormatter formatter) → void -
Writes a textual description of the state stree to the
sink. The specific output format is controlled by the type of theformatter. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
state(
StateKey stateKey, void build(StateBuilder builder), {StateKey? parent, InitialChild? initialChild}) → void -
Adds to the state tree a description of a state, identified by
stateKey. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
withDataRoot<
D> (StateKey rootState, InitialData< D> initialData, void build(DataStateBuilder<D> builder), InitialChild initialChild, {StateDataCodec? codec}) → StateTreeBuilder -
Creates a StateTreeBuilder with a root state carrying a value of type
D.
Constants
- defaultRootKey → const StateKey
-
The key identifying the root state that is implicitly added to a state tree, if the
new StateTreeBuilderconstructor is used.